uva 122 trees on the level——yhx

题目如下: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

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.

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 using namespace std;
 5 struct node
 6 {
 7     int lch,rch,val;
 8     bool b;
 9 }a[260],n1,n2;
10 int n,ans[260];
11 int rd()
12 {
13     int i,j,k,p,x,y,z,rt;
14     char s[300],c1,c2;
15     memset(a,0,sizeof(a));
16     n=1;
17     if (scanf("%s",s)==-1) return 0;
18     rt=1;
19     while (1)
20     {
21         if (s[1]==‘)‘) break;
22         x=0;
23         for (i=1;s[i]!=‘,‘;i++)
24           x=x*10+s[i]-‘0‘;
25         p=1;
26         for (i=i+1;i<=strlen(s)-2;i++)
27           if (s[i]==‘L‘)
28           {
29               if (!a[p].lch) a[p].lch=++n;
30               p=a[p].lch;
31           }
32           else
33           {
34               if (!a[p].rch) a[p].rch=++n;
35               p=a[p].rch;
36           }
37         if (a[p].b) rt=-1;
38         a[p].val=x;
39         a[p].b=1;
40         scanf("%s",s);
41     }
42     return rt;
43 }
44 queue<int> q;
45 int main()
46 {
47     int i,j,k,l,m,p,x,y,z;
48     bool b;
49     while (1)
50     {
51         x=rd();
52         if (!x) break;
53         if (x==-1)
54         {
55             printf("not complete\n");
56             continue;
57         }
58         while (!q.empty()) q.pop();
59         q.push(1);
60         memset(ans,0,sizeof(ans));
61         k=b=0;
62         while (!q.empty())
63         {
64             n1=a[q.front()];
65             q.pop();
66             if (!n1.b)
67             {
68                 b=1;
69                 break;
70             }
71             ans[++k]=n1.val;
72             if (n1.lch) q.push(n1.lch);
73             if (n1.rch) q.push(n1.rch);
74         }
75         if (b)
76           printf("not complete\n");
77         else
78         {
79             printf("%d",ans[1]);
80             for (i=2;i<=k;i++)
81                  printf(" %d",ans[i]);
82                printf("\n");
83         }
84     }
85 }

如果直接用数组下标表示位置,那么当所有节点连成一条线的时候下标将变得很大。所以需要在每个节点记录他的左右儿子位置,这样可以节省空出来的空间。

每个节点用一个bool记录是否被赋过值,如果没被赋过值或是被赋第二次值,那就not complete了。

但是注意的细节就是由于多组数据,即使读入时已经知道not complete也要读完。

时间: 2024-08-02 05:35:54

uva 122 trees on the level——yhx的相关文章

UVA 122 -- Trees on the level (二叉树 BFS)

 Trees on the level UVA - 122  解题思路: 首先要解决读数据问题,根据题意,当输入为"()"时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个readin()函数,类型设置为bool型,遇到第一种情况时返回true,遇到第二种情况返回false,主程序中只要发现readin返回false时就break,结束整个大循环. 接下来要建立二叉树,首先为二叉树编写一个结构体,然后根据字符串的输入情况来建树,如果是'L'就往左走,走不动时建一颗

UVa 122 Trees on the level(建树,层次遍历)

题意  建树并层次遍历输出  (data,pos)  pos表示改节点位置  L代表左儿子  R代表右儿子 建树很简单  开始在根节点  遇到L往左走遇到R往右走 节点不存在就新建   走完了就保存改节点的值  输出直接bfs就行了了 #include<cstdio> #include<cctype> #include<cstring> using namespace std; const int maxn = 300; char s[maxn][maxn]; int

UVa 122 Trees on the level

题意:给出一棵二叉树,按照从上到下,从左到右输出所有节点的值,如果有一个节点没有赋值或者被多次赋值则输出not complete 看的紫书照着敲的= = 先要将输入进来的值建成一颗二叉树(定义一个二叉树的节点,新建节点的函数,添加节点的函数),再对建好的二叉树遍历(BFS) 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<

UVA 122 Trees on the level 二叉树 广搜

题目链接: https://vjudge.net/problem/UVA-122 题目描述: 给你一种二叉树的构造方法, 让你逐层输出二叉树的节点值, 如果不能够则输出"not complete" 解题思路: 这道题就是硬搞就可以了, 参考紫书去做的, 首先处理输入就是非常麻烦的事情, 用到了sscanf就会轻松很多, 看来C中还是有很多很多的好用的标准库函数可以拿来用的, 例如还有本题中的strchr() , 处理完输入, 然后就去构造数. 然后广搜一遍即可 代码: #include

UVA - 122 Trees on the level Map

题目大意:给出一些字符串表示二叉树,字符串的格式如下(n,s),n表示节点上的数,s表示该节点在哪个位置,问这些字符串能否组成二叉树 解题思路:能否组成二叉树,首先需要判断根节点. 接着判断一下其他节点,按层次遍历的顺序来枚举,所以要先排序,按字符串的长度来排序. 如果该节点能是二叉树的节点,那么他的父节点就要存在 判断他的父节点是否存在,只需判断他的前len-1(len表示长度)个字符组成的串是否存在 这题注意要判重 #include<cstdio> #include<cstring&

uva 122 - Trees on the level(一棵看着书都写不利索的树……)

#include<cstdio> #include<iostream> #include<cstring> #include<vector> #include<queue> #include<algorithm> using namespace std; const int maxn = 1000; char s[maxn]; bool failed; vector<int > ans; struct node { boo

UVa 122 Trees on the level (动态建树 &amp;&amp; 层序遍历二叉树)

题意  :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻结点之间用一个空格隔开.每棵树的输入用一对空括 号"()"结束(这对括号本身不代表一个结点),注意,如果从根到某个叶结点的路径上有的结点没有在输入中给出,或者给出超过一 次,应当输出not complete.结点个数不超过256. 分析  : 如果使用数组建树的话,256个结点看着不多,但

122 - Trees on the level(动态分配空间解法)

Trees on the level Background 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

UVa122:Trees on the level

Trees on the level Background 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