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 ;
}
 

Uva122 Trees on the level的更多相关文章

  1. uva-122 Trees on the level(树的遍历)

    题目: 给出一棵树的表示,判断这棵树是否输入正确,如果正确就按层次遍历输出所有的结点,错误的话就输出not complete. 思路: 根据字符串中树的路径先将树建起来,在增加结点和层次遍历树的时候判 ...

  2. Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。

    Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...

  3. E - Trees on the level

     Trees on the level  Background Trees are fundamental in many branches of computer science. Current ...

  4. Trees on the level(指针法和非指针法构造二叉树)

    Trees on the level Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. hdu 1622 Trees on the level(二叉树的层次遍历)

    题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...

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

    UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...

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

     Trees on the level UVA - 122  解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...

  8. 例题6-7 Trees on the level ,Uva122

    本题考查点有以下几个: 对数据输入的熟练掌握 二叉树的建立 二叉树的宽度优先遍历 首先,特别提一下第一点,整个题目有相当一部分耗时在了第一个考查点上(虽然有些不必要,因为本应该有更简单的方法).这道题 ...

  9. Trees on the level (二叉链表树)

    紫书:P150 uva122 Background Trees are fundamental in many branches of computer science. Current state- ...

随机推荐

  1. 第八届河南省省赛 A.挑战密室

    挑战密室 时间限制: ms | 内存限制: KB 难度: 描述 R组织的特工Dr. Kong 为了寻找丢失的超体元素,不幸陷入WTO密室.Dr. Kong必须尽快找到解锁密码逃离,否则几分钟之后,WT ...

  2. [SDOI2013]泉

    题目描述 作为光荣的济南泉历史研究小组中的一员,铭铭收集了历史上x个不同年份时不同泉区的水流指数,这个指数是一个小于. 2^30的非负整数.第i个年份时六个泉区的泉水流量指数分别为 A(i,l),A( ...

  3. POJ 3608 旋转卡壳

    思路: 旋转卡壳应用 注意点&边  边&边  点&点 三种情况 //By SiriusRen #include <cmath> #include <cstdi ...

  4. 2017杭电多校第六场03Inversion

    传送门 Inversion Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  5. python程序展现图片

    突然想写一个python程序能够显示图片的 ,展示文字的已经实现了 现在就搞一搞这个吧 相信也是很简单 首先是放一张图片在e盘下面 等会程序打包的时候将会用到 就决定是你啦 皮卡丘: 然后就写代码吧:

  6. [转]ASP.NET MVC的帮助类HtmlHelper和UrlHelper

    本文转自:http://www.cnblogs.com/greatandforever/archive/2010/04/20/1715914.html?login=1 在ASP.NET MVC框架中没 ...

  7. Redis安全与持久化(适合小白阅读)

    前言:Redis的使用越来越重要.以下仅为个人学习的点点记录.仅供参考. 一.简单的redis安全性设置 1. 生产环境的redis最好建议在redis配置文件中设置bind.配置允许指定的ip登陆r ...

  8. webview页面间的通信问题

    前提 记一次多页面开发. 开发需求时会对页面刷新(reload),返回到上一页(用户返回 / history.go()) 页面间的通信 sessionStorage保存本次会话的信息,同步到新页面或上 ...

  9. TCP/IP和UDP的比较

    TCP.UDP详解 1.传输层存在的必要性 由于网络层的分组传输是不可靠的,无法了解数据到达终点的时间,无法了解数据未达终点的状态.因此有必要增强网络层提供服务的服务质量. 2.引入传输层的原因 面向 ...

  10. (转)Hibernate框架基础——映射集合属性

    http://blog.csdn.net/yerenyuan_pku/article/details/52745486 集合映射 集合属性大致有两种: 单纯的集合属性,如像List.Set或数组等集合 ...