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.
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node
{
int lch,rch,val;
bool b;
}a[],n1,n2;
int n,ans[];
int rd()
{
int i,j,k,p,x,y,z,rt;
char s[],c1,c2;
memset(a,,sizeof(a));
n=;
if (scanf("%s",s)==-) return ;
rt=;
while ()
{
if (s[]==')') break;
x=;
for (i=;s[i]!=',';i++)
x=x*+s[i]-'';
p=;
for (i=i+;i<=strlen(s)-;i++)
if (s[i]=='L')
{
if (!a[p].lch) a[p].lch=++n;
p=a[p].lch;
}
else
{
if (!a[p].rch) a[p].rch=++n;
p=a[p].rch;
}
if (a[p].b) rt=-;
a[p].val=x;
a[p].b=;
scanf("%s",s);
}
return rt;
}
queue<int> q;
int main()
{
int i,j,k,l,m,p,x,y,z;
bool b;
while ()
{
x=rd();
if (!x) break;
if (x==-)
{
printf("not complete\n");
continue;
}
while (!q.empty()) q.pop();
q.push();
memset(ans,,sizeof(ans));
k=b=;
while (!q.empty())
{
n1=a[q.front()];
q.pop();
if (!n1.b)
{
b=;
break;
}
ans[++k]=n1.val;
if (n1.lch) q.push(n1.lch);
if (n1.rch) q.push(n1.rch);
}
if (b)
printf("not complete\n");
else
{
printf("%d",ans[]);
for (i=;i<=k;i++)
printf(" %d",ans[i]);
printf("\n");
}
}
}
如果直接用数组下标表示位置,那么当所有节点连成一条线的时候下标将变得很大。所以需要在每个节点记录他的左右儿子位置,这样可以节省空出来的空间。
每个节点用一个bool记录是否被赋过值,如果没被赋过值或是被赋第二次值,那就not complete了。
但是注意的细节就是由于多组数据,即使读入时已经知道not complete也要读完。
uva 122 trees on the level——yhx的更多相关文章
- UVA.122 Trees on the level(二叉树 BFS)
UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...
- UVA 122 -- Trees on the level (二叉树 BFS)
Trees on the level UVA - 122 解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...
- UVa 122 Trees on the level(二叉树层序遍历)
Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...
- UVa 122 Trees on the level
题目的意思: 输入很多个节点,包括路径和数值,但是不一定这些全部可以构成一棵树,问题就是判断所给的能否构成一棵树,且没有多余. 网上其他大神已经给出了题目意思:比如我一直很喜欢的小白菜又菜的博客 说一 ...
- UVa 122 Trees on the level(链式二叉树的建立和层次遍历)
题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优 ...
- UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)
题意 :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...
- UVA - 122 Trees on the level (二叉树的层次遍历)
题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...
- 内存池技术(UVa 122 Tree on the level)
内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时, ...
- Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...
随机推荐
- C#从服务器下载文件到客户端源码
1.在window窗体加个button控件,双击进去
- sql server2008中怎样用sql语句创建数据库和数据表
这是简单用代码实现创建数据库和数据表的sql语句,如下: --调用系统数据库-- use master go /***防止你要创建的数据库同名,先把它删除掉****/ if Exists(select ...
- 重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu
[源码下载] 重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu 作者 ...
- SqlServer常用语句参考
1.SQL SERVER中如何使用SQL 语句复制表结构: select * into 新表 from 旧表 where 1=2 把“旧表”的表结构复制到“新表”,1=2不复制数据,如果要复制数据,就 ...
- scons学习
http://blog.csdn.net/andyelvis/article/category/948141 http://www.scons.org/doc/production/HTML/scon ...
- Chrome浏览器的Timing分析
以W3C网站为例: Stalled是浏览器得到要发出这个请求的指令,到请求可以发出的等待时间,一般是代理协商.以及等待可复用的TCP连接释放的时间,不包括DNS查询.建立TCP连接等时间等. SSL( ...
- c++中stl容器的常用示例
1. set(集合)——包含了经过排序了的数据,这些数据的值(value)必须是唯一的. 也就是说输入set容器后得到数据,会去重并排序. s.insert()插入一个元素 s.begin ...
- [moka同学笔记]一、Yii2.0课程笔记(魏曦老师教程)
第一节 第二节 课程内容
- Verilog学习笔记基本语法篇(十)········ 常用系统函数
$display 和 $write 任务 格式: $display (p1,p2,...,pn); $write (p1,p2,..,pn); 这两个函数和系统的任务作用是用来输出信息,即将参数p2到 ...
- dubbo序列化的一点注意
最近工作中遇见了一个小问题,在此记录一下,大致是这样的,有一父类,有一个属性traceId,主要是记录日志号,这样可以把所有日志串起来,利于排查问题,所有的pojo对象继承于此,但是其中一同事在子类p ...