HDU - 1622 Trees on the level

题目链接

题意:

一个序列中的每一棵树都由若干对(n,s)组成用空格分隔。每棵树中的最后一个条目是()。左括号和右括号之间不显示空白。 所有节点都包含一个正整数。输入中的每棵树将至少由一个节点和不超过256个节点组成。输入以文件结尾终止。 对于输入文件中每个完全指定的二叉树,打印该树的层序遍历。如果没有完全指定树,即树中的某个节点没有给定值,或者某个节点被多次给定值,则应打印字符串“未完成”

(弱鸡英语看不懂题目,o ( ╥ ﹏ ╥ ) o ,错过了很多细节,WA了很多次,所以做题前务必读三遍题)

思路:

比较基础的一道题目,二叉树的建树与层序遍历,注意其中的一些小细节

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cstdlib>
#include<cstring>
#include<queue>
using namespace std;

struct node
{
    int data;
    node *lchild,*rchild;
};

queue<node*>q;
char s[1010];
char Map[1010];
int flag;
int pr[1010];
int tp;

node* newNode(int v)//建立新结点
{
    node* Node=new node;
    Node->data=v;
    Node->lchild=Node->rchild=NULL;
    return Node;
}

void Insert(node* root,int x,int level)
{
    for(int i=0;i<level;i++)
    {
        if(Map[i]=='L')
        {
        if(root->lchild==NULL)
            root->lchild=newNode(-1);
        root=root->lchild;
        }
        else
       {
        if(root->rchild==NULL)
            root->rchild=newNode(-1);
        root=root->rchild;
       }
    }  
    if(root->data!=-1)
        flag=0;//插入时判断某个节点是否被多次给定值    
            
    root->data=x;
}

void bfs(node* root)
{
    tp=0;  
    if(root->data==-1)
        flag=0;//根结点是否给定值
        
    q.push(root);
    while(!q.empty())
    {
        node* now=q.front();
        q.pop();
        pr[tp++]=now->data;        
        if(now->data==-1)
            flag=0;//树中的某个节点是否给定值
            
        if(now->lchild!=NULL)
            q.push(now->lchild);
        if(now->rchild!=NULL)
            q.push(now->rchild);
    }
}

int main()
{
    int sum,j,level;
    while(scanf("%s",s)!=EOF)//输入以文件结尾终止
    {
        flag=1;
        node* root=newNode(-1);
        while(1)
        {
            for(int i=0;i<strlen(s);i++)
            {
                if(s[i]!='(')
                    continue;
                sum=0,j=i+1,level=0;
                while(s[j]!=')')
                {
                    if(s[j]>='0'&&s[j]<='9')
                    {
                        sum=sum*10+(s[j]-'0');
                    }
                    else if(s[j]!=',')
                        Map[level++]=s[j];
                    j++;
                }
                if(sum!=0)
                {
                    Insert(root,sum,level);
                }
            }
            if(s[strlen(s)-2]=='('&&s[strlen(s)-1]==')')
                break;
                
            scanf("%s",s);//输入当读取空格时输入结束,所以要重新输入
            
        }
        bfs(root);
          if(!flag)
            printf("not complete\n");
          else
        {
            int flag2=1;
            for(int i=0;i<tp;i++)
            {
                if(flag2==1)
                {
                 printf("%d", pr[i]);
                 flag2=0;
                }
                 else
                 printf(" %d",pr[i]);
            }
            cout<<endl;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值