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 computer graphics.

This problem involves building and traversing binary trees.

The Problem

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 andR'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.

The 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.

The 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
题意:输入一个二叉树,要求从上到下,从左到右输出各个节点的值.
分析:一层一层地输出,很显然,就是bfs,关键就是细节问题.
首先读入字符串,这个读入比较烦人,可能有多组数据,还要判断结束,那么先判断能不能读入,然后再来判断是否读入结束.
下面考虑该如何建树,显然不能直接按照编号来,最多256个点,如果成了一条链那么编号的大小就高达2^256,那么每个点保存一下左右儿子就好了,在读入的过程中动态建点.
一些逻辑语句的问题不能再犯了,哪怕写长一点,多加一对括号都可以的.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue> using namespace std; int flag,ans[],top,cnt,p,num,fanhui;
bool flg = false; struct node
{
int l, r, v;
bool vis;
}e[]; int read()
{
char ch[];
memset(e, , sizeof(e));
cnt = ;
if (scanf("%s", ch) == -)
return ;
fanhui = ;
int i;
while ()
{
if (ch[] == ')')
break;
num = ;
for (i = ; ch[i] != ','; i++)
num = num * + ch[i] - '';
p = ;
for (i = i + ; i < strlen(ch) - ; i++)
{
if (ch[i] == 'L')
{
if (!e[p].l)
e[p].l = ++cnt;
p = e[p].l;
}
else
{
if (!e[p].r)
e[p].r = ++cnt;
p = e[p].r;
}
}
if (e[p].vis)
fanhui = -;
e[p].v = num;
e[p].vis = ;
scanf("%s", ch);
}
return fanhui;
} int main()
{
while ()
{
flag = read();
if (!flag)
break; if (flag == -)
{
printf("not complete\n");
continue;
} queue <int>q;
q.push();
memset(ans, , sizeof(ans));
top = ;
flg = false;
while (!q.empty())
{
node u = e[q.front()];
q.pop();
if (!u.vis)
{
flg = true;
break;
}
ans[++top] = u.v;
if (u.l)
q.push(u.l);
if (u.r)
q.push(u.r);
}
if (flg)
printf("not complete\n");
else
{
for (int i = ; i <= top; i++)
{
if (i != top)
printf("%d ", ans[i]);
else
printf("%d", ans[i]);
}
printf("\n");
}
} return ;
}
 

最新文章

  1. emacs设置代理访问插件仓库
  2. net start mysql服务名无效
  3. [原创]一个纯css实现兼容各种主流移动pc浏览器的时间轴
  4. delphi xe5 android 开发数据访问手机端 解决乱码的办法
  5. 富文本文件CKEDITOR增加上传图片功能(.net)
  6. JVM学习之JVM1.6 GC详解
  7. 用Servlet实现聊天室设计
  8. Exception和RuntimeException
  9. hdoj 2121 Ice_cream’s world II 【没有最低树的根节点】
  10. 生成 HTMLTestRunner 测试报告学习总结
  11. 关于极光推送Jpush的demo
  12. nodeJs多进程Cluster
  13. Linux0.11进程切换和TSS结构
  14. C++设计模式之工厂模式(1)
  15. ThinkPHP框架 表单传值自动验证!!
  16. easyui combotree combobox 使用例子
  17. Python3 文件操作(十六)
  18. cocos2dx 3.2 解决输入框(TextField,TextFieldTTF) 中文乱码问题
  19. 程序员不常用Linux命令集
  20. 使用BBED理解和修改Oracle数据块

热门文章

  1. 疫情控制 2012年NOIP全国联赛提高组(二分答案+贪心)
  2. 使用JS分页 &lt;span&gt; beta 3.0 完成封装的分页
  3. 所有版本chrome、chromedriver、firefox下载链接
  4. 全面学习ORACLE Scheduler特性(10)管理Chains
  5. Spring-security自定义过滤器
  6. MySQL的两种存储引擎storage engine特点和对比
  7. javascript学习之Date对象
  8. idea安装插件plugin(主要针对网络连接不上的情况)
  9. MySQL 帮助类 MySQLHelper
  10. MySQL 8.*版本 修改root密码