UVa 122 Trees on the level(二叉树层序遍历)
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.
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.
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.
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
题意
把给你的所有结点转成一棵树,并按层次遍历输出
题解
用链表的就不多说了,贴个数组的
代码
#include<bits/stdc++.h>
using namespace std;
int Left[],Right[],V[],failed,cnt;
vector<int> vec;
void addnode(int v,char *s)
{
int n=strlen(s);//s[0]=','后的一个字符
int u=;
for(int i=;i<n;i++)
{
if(s[i]=='L')
{
if(Left[u]==-)
{
++cnt;
Left[cnt]=Right[cnt]=V[cnt]=-;
Left[u]=cnt;
}
u=Left[u];
}
else if(s[i]=='R')
{
if(Right[u]==-)
{
++cnt;
Left[cnt]=Right[cnt]=V[cnt]=-;
Right[u]=cnt;
}
u=Right[u];
}
else break;
}
if(V[u]!=-)failed=;//节点值已经存在
V[u]=v;
}
int bfs()
{
vec.clear();
queue<int> qu;
qu.push();
while(!qu.empty())
{
int top=qu.front();qu.pop();
if(V[top]==-)return ;//节点没有赋值
vec.push_back(V[top]);
if(Left[top]!=-)qu.push(Left[top]);
if(Right[top]!=-)qu.push(Right[top]);
}
return ;
}
int read()
{
int v;
char s[];
Left[]=Right[]=V[]=-;//0为根节点
failed=;
cnt=;
for(;;)
{
if(scanf("%s",s)!=)return ;
if(!strcmp(s,"()"))break;
sscanf(&s[],"%d",&v);//从s[1]开始读一个数字
addnode(v,strchr(s,',')+);
}
return ;
}
int main()
{
while(read())
{
if(failed==||bfs()==)
printf("not complete\n");
else
for(int i=;i<vec.size();i++)
printf("%d%c",vec[i],i==vec.size()-?'\n':' ');
}
return ;
}
UVa 122 Trees on the level(二叉树层序遍历)的更多相关文章
- UVA.122 Trees on the level(二叉树 BFS)
UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...
- UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)
题意 :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...
- UVA 122 -- Trees on the level (二叉树 BFS)
Trees on the level UVA - 122 解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...
- UVa 122 Trees on the level(链式二叉树的建立和层次遍历)
题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优 ...
- UVA - 122 Trees on the level (二叉树的层次遍历)
题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...
- uva 122 trees on the level——yhx
题目如下:Given a sequence of binary trees, you are to write a program that prints a level-order traversa ...
- UVa 122 Trees on the level
题目的意思: 输入很多个节点,包括路径和数值,但是不一定这些全部可以构成一棵树,问题就是判断所给的能否构成一棵树,且没有多余. 网上其他大神已经给出了题目意思:比如我一直很喜欢的小白菜又菜的博客 说一 ...
- 【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】
[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a ...
- [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
随机推荐
- Group by与 having
注意:select 后的字段,必须要么包含在group by中,要么包含在having 后的聚合函数里. 1. GROUP BY 是分组查询, 一般 GROUP BY 是和聚合函数配合使用 group ...
- +load +initialize
+load方法 在app启动的时候各个类的+load方法都会被调用,+load方法不是通过消息机制调用的,它是直接调用的,因此无论是在子类或者category中复写此方法,复写的+load方法都会被调 ...
- 【坑】tableView cell默认选中
在tableView展示的过程时候,如果想一开始就有一些cell默认被选中,不能在cellForRowAtIndexPath中cell.selected=YES, 必须在willDisplayCell ...
- Delphi 字符串截取函数
如果要使用LeftStr,RightStr,MidStr必需引用系统单元StrUtils; 声明变量Str:string; Str:=HelloWorld; 1,LeftStr(Str,2)=He;/ ...
- KVM虚拟化技术(一)虚拟化简介
一 .虚拟化 虚拟化是指计算机元件在虚拟的基础上而不是真实的基础上运行.虚拟化技术可以扩大硬件的容量,简化软件的重新配置过程.CPU的虚拟化技术可以单CPU模 拟多CPU并行,允许一个平台同时运行多个 ...
- jndi 小案例
JNDI就是为JAVA中命名和目录服务定义的JAVA API,是命名服务的抽象机制.在J2EE中,JNDI的目的是用来查找J2EE服务器的注册资源.只要该对象在命名服务器上注册过,且你知道命名服务器的 ...
- Celery 图,[转]
https://www.cnblogs.com/forward-wang/p/5970806.html
- python3.6安装-windows
1.打开python官网 2.找到下载链接 3.选择对应的版本下载 4.下载完成后打开安装包并执行,运行出该界面. 5.这里是安装到C盘上(默认安装) 6.此处为自定义安装 7.选择自定义安装,并全选 ...
- js高级-函数变量提升
var a = 10; function f1(){ console.log(a) //undefined 函数变量提升了 函数执行之前想创建了函数的EC 把函数里面声明的变量初始化undefine ...
- MATLAB GUI图片添加背景
global im [filename,pathname]=uigetfile('*.jpg','输入图片'); file=strcat(pathname,filename); im=imread(f ...