Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state-
of-the art parallel computers such as Thinking Machines’ CM- 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 pro-
gram 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 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
+
.
For example, a level order traversal of the tree on the right
is: , , , , , , , , .
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 is specified by
(,RL)
, and the node containing is
specified by
(,LLR)
. The root node is specified by
(,)
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 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
(,LL) (,LLL) (,R)
(,) (,L) (,RL) (,LLR) (,RRR) (,RR) ()
(,L) (,R) ()
Sample Output not complete /**
题目:Trees on the level UVA - 122
链接:https://vjudge.net/problem/UVA-122
题意:lrj算法竞赛入门经典P150. eg6-7
分析:
建造一颗二叉树。链表或者数组方式。 本题不可以数组方式,因为节点个数达到256个,当为一条链的时候,2^256超大。 采用树结构。 收获:复习二叉树的建立,使用。
sscanf和strchr的使用。 sscanf(&s[1],"%d",&value);表示把一个字符串当做输入,输入到后面"%d",&value中读取。
strchr(s,',')表示返回s串中第一次出现','的指针。
*/ #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
const int maxn = 1e5+;
const int mod = 1e9+;
int sign; /// 1 表示同一节点重复出现过。
struct node
{
int value;
int exist;/// 1 表示存在。
node *left, *right;
node():exist(),left(NULL),right(NULL){}
};
char s[];
char out[]="not complete";
node* root = new node();
void dfs(int value,char *s)
{
node* u = root;
for(int i = ; s[i]!='\0'; i++){
if(s[i]=='L'){
if(u->left==NULL) u->left = new node();
u = u->left;
}else
{
if(s[i]=='R'){
if(u->right==NULL) u->right = new node();
u = u->right;
}else
{
if(u->exist==) sign = ;
u->value = value;
u->exist = ;
}
}
}
}
vector<int> ans;
void bfs()
{
node* u = root;
queue<node*> qu;
ans.clear();
qu.push(u);
while(!qu.empty()){
u = qu.front();
qu.pop();
if(u->exist==){
sign = ; break;
}
ans.push_back(u->value);
if(u->left!=NULL){
qu.push(u->left);
}
if(u->right!=NULL){
qu.push(u->right);
}
} if(sign){
cout<<out<<endl; return ;
}
int len = ans.size();
printf("%d",ans[]);
for(int i = ; i < len; i++){
printf(" %d",ans[i]);
}
cout<<endl;
}
int main()
{
while(scanf("%s",s)!=EOF)
{
int value;
if(strcmp(s,"()")==) continue;
root = new node();
sign = ;
sscanf(&s[],"%d",&value);
dfs(value,strchr(s,',')+);
//@Test //sscanf(&s[1],"%d",&value);
//cout<<"value = "<<value<<endl;
while(scanf("%s",s)!=EOF){
if(strcmp(s,"()")==){
bfs();
break;
}
sscanf(&s[],"%d",&value);
dfs(value,strchr(s,',')+);
}
}
return ;
}
Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。的更多相关文章
- Trees on the level UVA - 122 (二叉树的层次遍历)
题目链接:https://vjudge.net/problem/UVA-122 题目大意:输入一颗二叉树,你的任务是按从上到下,从左到右的顺序输出各个结点的值.每个结点都按照从根节点到它的移动序列给出 ...
- Trees on the level UVA - 122
Trees are fundamental in many branches of computer science (Pun definitely intended). Current stateo ...
- 【紫书】Trees on the level UVA - 122 动态建树及bfs
题意:给你一些字符串,代表某个值被插入树中的位置.让你输出层序遍历. 题解:动态建树. 由于输入复杂,将输入封装成read_input.注意输入函数返回的情况 再将申请新节点封装成newnode(). ...
- UVa 122 Trees on the level(链式二叉树的建立和层次遍历)
题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优 ...
- UVA 122 -- Trees on the level (二叉树 BFS)
Trees on the level UVA - 122 解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...
- 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表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...
- 【例题 6-7 UVA - 122 】Trees on the level
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 二叉树的话,直接用数组存就好了. 写个bfs记录一下答案. [代码] #include <bits/stdc++.h> ...
- Trees on the level(指针法和非指针法构造二叉树)
Trees on the level Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- AngularJS的加载执行过程
1. HTML页面的加载,这会触发加载页面包含的所有JS (包括 AngularJS) 2. AngularJS启动,搜寻所有的指令(directive) 3. 找到ng-app,搜寻其指定的模块(M ...
- SSH学习——声明式事物管理(Spring)
1.什么是事物? 事务是一组操作的执行单元,相对于数据库操作来讲,事务管理的是一组SQL指令,比如增加,修改,删除等,事务的一致性,要求,这个事务内的操作必须全部执行成功,如果在此过程种出现了差错,比 ...
- display:flex;多行多列布局学习
从以前的table布局到现在的div布局,再到未来的flex布局,CSS重构方面对展示行和适应性的要求越来越高: 首先来比较一下布局方式的更新意义: table布局: 优点:1.兼容性好,ie6.ie ...
- python中下划线_的用途
Python 用下划线作为变量前缀和后缀指定特殊变量. _xxx 不能用'from module import *'导入 __xxx__ 系统定义名字 __xxx 类中的私有变量 ...
- C#的Xamarin开发小米盒子应用并以WCF实现微信通知
对于熟悉C#语言的开发人员而言,用Xamarin开发Android应用也是一个不错的选择.小米盒子是Android系统.当然也就能够使用Xamarin来开发.首选来看效果图. watermark/2/ ...
- 2017.11.21 postgre更新时需要联合其他表的信息
现在需要更新t_user表,但是前台传来的参数 tenant_name 并不在这个表中,需要联合另一个表t_tenant. 要注意的一点是:set后面的字段不要写成 u.fd_validity,否则会 ...
- python获取当前文件路径
python获取当前文件路径 学习了:https://www.cnblogs.com/strongYaYa/p/7200357.html https://blog.csdn.net/heatdeath ...
- IntelliJ IDEA 控制台中文乱码
1. 预热 刚刚接触IntelliJ IDEA几天,在易用性方面的确比Eclipse好很多,比较智能,各种插件.工具都已经集成,和Mac OS X类似——开箱即用. 但是还是老大难问题——中文乱码,让 ...
- webstorm 破解方式
注册时,在打开的License Activation窗口中选择“License server”,在输入框输入下面的网址: http://idea.iteblog.com/key.php 点击:Acti ...
- JAVA实现https单向认证
//关于http 须要两个jar包 httpclient-4.0.jar httpcore-4.0.1.jar private static final HttpClient httpClient = ...