Trees on the level HDU - 1622 二叉树

问题:

Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines' CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in computer graphics. 

This problem involves building and traversing binary trees. 
Given a sequence of binary trees, you are to write a program that prints a level-order traversal of each tree. In this problem each node of a binary tree contains a positive integer and all binary trees have have fewer than 256 nodes. 

In a level-order traversal of a tree, the data in all nodes at a given level are printed in left-to-right order and all nodes at level k are printed before all nodes at level k+1. 

For example, a level order traversal of the tree 

                                                               Trees on the level HDU - 1622  二叉树 
                                                                             is: 5, 4, 8, 11, 13, 4, 7, 2, 1. 

In this problem a binary tree is specified by a sequence of pairs (n,s) where n is the value at the node whose path from the root is given by the string s. A path is given be a sequence of L's and R's where L indicates a left branch and R indicates a right branch. In the tree diagrammed above, the node containing 13 is specified by (13,RL), and the node containing 2 is specified by (2,LLR). The root node is specified by (5,) where the empty string indicates the path from the root to itself. A binary tree is considered to be completely specified if every node on all root-to-node paths in the tree is given a value exactly once. 
 

Input

The input is a sequence of binary trees specified as described above. Each tree in a sequence consists of several pairs (n,s) as described above separated by whitespace. The last entry in each tree is (). No whitespace appears between left and right parentheses. 

All nodes contain a positive integer. Every tree in the input will consist of at least one node and no more than 256 nodes. Input is terminated by end-of-file. 
 

Output

For each completely specified binary tree in the input file, the level order traversal of that tree should be printed. If a tree is not completely specified, i.e., some node in the tree is NOT given a value or a node is given a value more than once, then the string ``not complete'' should be printed

Sample Input

(11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()

Sample Output

5 4 8 11 13 4 7 2 1
not complete

题意:(a,b):路径b的节点值是a。(a,):a是根节点。()数据输入结束。L表示向

左插入,R表示向右插入。判断是否构成一颗树,如果是则按照每层顺序显示节点,如果不

是则输出not complete。

思路:直接建树,建完树用个队列判断一下是否都连通。

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
    int x,tag=0;
    node *l=NULL,*r=NULL;
} *tree;
int book;
int t,aa[12000];
char s1[12000],s2[12000];
void build(int h,char s2[])
{
    int i;
    node *cur=tree;
    for(i=0; s2[i]!='\0'; i++)
    {
        if(s2[i]=='L')
        {
            if(cur->l==NULL)
                cur->l=new node;
            cur=cur->l;
        }
        else
        {
            if(cur->r==NULL)
                cur->r=new node;
            cur=cur->r;
        }
    }
    if(cur->tag)book=1;
    cur->x=h;
    cur->tag=1;
}
void find1()
{
    node *cur=tree;
    queue<node*>q;
    q.push(tree);
    int t=0,flag=0;
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        if(cur->tag==0)
        {
            flag=1;
            break;
        }
        aa[t++]=cur->x;
        if(cur->l!=NULL)
            q.push(cur->l);
        if(cur->r!=NULL)
            q.push(cur->r);
    }
    if(flag==0&&book==0)
    {
        for(int i=0; i<t; i++)
        {
            if(i==0)
                printf("%d",aa[i]);
            else
                printf(" %d",aa[i]);
        }
    }
    else
        printf("not complete");
    printf("\n");
}
void free1(node *cur)
{
    if(cur==NULL)
        return ;
    free1(cur->l);
    free1(cur->r);
    free(cur);
}
int main()
{
    char s[12000];
    book=0;
    tree=new node;
    while(~scanf("%s",s))
    {
        if(s[0]=='('&&s[1]==')')
        {
            find1();
            book=0;
            free1(tree);
            tree=new node;
            continue;
        }
        int d=strlen(s),sum=0,t=0;
        for(int i=1; i<d-1; i++)
        {
            if('0'<=s[i]&&s[i]<='9')
                sum=sum*10+s[i]-48;
            else if(s[i]==',')
            {
                for(int j=i+1; j<d-1; j++)
                    s1[t++]=s[j];
                s1[t]='\0';
                break;
            }
        }
        build(sum,s1);
    }
    return 0;
}

 

上一篇:ArcGIS version not specified.


下一篇:代码安全 | 第十二期:LDAP注入漏洞