题目如下: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的更多相关文章

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

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

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

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

  3. UVa 122 Trees on the level(二叉树层序遍历)

    Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...

  4. UVa 122 Trees on the level

    题目的意思: 输入很多个节点,包括路径和数值,但是不一定这些全部可以构成一棵树,问题就是判断所给的能否构成一棵树,且没有多余. 网上其他大神已经给出了题目意思:比如我一直很喜欢的小白菜又菜的博客 说一 ...

  5. UVa 122 Trees on the level(链式二叉树的建立和层次遍历)

    题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优 ...

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

    题意  :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...

  7. UVA - 122 Trees on the level (二叉树的层次遍历)

    题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...

  8. 内存池技术(UVa 122 Tree on the level)

    内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时, ...

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

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

随机推荐

  1. C#从服务器下载文件到客户端源码

    1.在window窗体加个button控件,双击进去

  2. asp.net.mvc4在vs2010怎样创建mvc项目及它的结构

    1.打开vs2012,创建mvc项目 文件-->新建--> 项目--> web--> asp.net.Mvc 4web应用程序-->基本模板

  3. Verilog学习笔记设计和验证篇(二)...............同步有限状态机

    上图表示的就是数字电路设计中常用的时钟同步状态机的结构.其中共有四个部分产生下一状态的组合逻辑F.状态寄存器组.输出组合逻辑G.流水线输出寄存器组.如果状态寄存器组由n个寄存器组成,就可以记忆2^n个 ...

  4. Selenium&EmguCV实现爬虫图片识别

    概述 爬虫需要抓取网站价格,与一般抓取网页区别的是抓取内容是通过AJAX加载,并且价格是通过CSS背景图片显示的. 每一个数字对应一个样式,如'p_h57_5' .p_h57_5 { backgrou ...

  5. SQLserver2008如何把表格变量传递到存储过程中

    在Microsoft SQL Server 2008中,你可以实现把表格变量传递到存储过程中,如果变量可以被声明,那么它就可以被传递.下面我们来具体介绍如何把表格变量(包括内含的数据)传递到存储过程和 ...

  6. Virtual DOM 算法

    前端 virtual-dom react.js javascript 目录: 1 前言 2 对前端应用状态管理思考 3 Virtual DOM 算法 4 算法实现 4.1 步骤一:用JS对象模拟DOM ...

  7. jQuery源码分析-01总体架构

    1. 总体架构 1.1自调用匿名函数 self-invoking anonymous function 打开jQuery源码,首先你会看到这样的代码结构: (function( window, und ...

  8. vi编辑器常用配置

    在终端下使用vim进行编辑时,默认情况下,编辑的界面上是没有显示行号.语法高亮度显示.智能缩进等功能的.为了更好的在vim下进行工作,需要手动设置一个配置文件:.vimrc. 在启动vim时,当前用户 ...

  9. 使用SharedPreferences进行简单的储存

    博客地址 http://www.cnblogs.com/mmyblogs/p/6082512.html(转载请保留) SharedPreferences定义 1.是一种轻型的数据存储的方式 2.本质是 ...

  10. Mac上的软件的一些对开发者有用的使用技巧(持续更新)

    内容大纲: Google浏览器设置开发者模式 正文: 1.Google浏览器设置开发者模式