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 ...
随机推荐
- mui init 出现无法引入子页面问题
1. 检查项目中是否重复出现了 mui.init() 函数; mui.init({ subpages: [{ styles: { // top: "44px", top: &quo ...
- es6 初级之箭头函数
1.先看一个例子: <script> function show() { console.log('aluoha'); } show(); </script> 2. 改写成简单 ...
- 2.ECS内 经典网络与VPC网络的区别
对应阿里云学习视频地址:https://help.aliyun.com/video_detail/67686.html?spm=5176.208355.1107604.1.10d82ca1J91Ckt ...
- linux mce的一些相关内容和用户态监控的设计方法
之所以想起写一点关于mce的东西,倒不是因为遇到mce的异常了,之前遇到过很多mce的异常,内存居多,但没有好好记录下来,写这个是因为参加2018 clk南京会议的一点想法. void __init ...
- Shell函数使用方法
Shell函数是一组命令集或语句组成一个可用块.利用函数可以简化脚本编写.函数要求先定义再使用,调用函数时直接使用函数名即可.这里主要介绍shell编程中函数定义.调用.获取函数参数以及获取函数返回值 ...
- 【原】Ubuntu virtual terminal
CTRL+ALT+F1 ~ F6 six virtual terminal ALT-F7 return to graphic desktop
- Hibernate 再接触 组件映射
将另外一个类嵌入到另外一个类 从而合并生成一张表 Husband.java package com.bjsxt.hibernate; import javax.persistence.Embedded ...
- ArcGIS案例学习笔记3_1_地理配准案例_图面控制点
ArcGIS案例学习笔记3_1_地理配准案例_图面控制点 计划时间:第3天上午 目的:地形图控制点配准 数据:地形图drg 无坐标: 步骤 1.查看地图标注 2. 地理配准,添加控制点 3.结果: 联 ...
- IE (第二部分) 浏览器 中 关于浏览器模式和文本模式
判断真正的 IE 版本 很多 JS 框架都通过 UA 判断 IE 的版本.对于 IE6,这种做法没问题( IE6 没有浏览器模式的概念,也没有其它 IE 可以把浏览器模式改为 IE6:IE7 虽然也没 ...
- ArrayList删除--------ConcurrentModificationException问题
在做项目中用到List存储数据,在里面做数据操作时候用到了删除.结果抛出ConcurrentModificationException异常.在这里把问题总结一下. 原因: ArrayList进行for ...