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 ...
随机推荐
- hidefocus小技巧
hidefocus即隐藏聚焦,具有使对象聚焦失效的功能,其功能相当于: onFocus="this.blur()" 它的值是一个布尔值,如 hidefocus="true ...
- How to enable Hibernate option in windows 2008 R2 server?
http://velshare.blogspot.com/2013/02/how-to-enable-hibernate-option-in.html 1) To enable the Hiberna ...
- Delphi 通过SQLite3, SQLiteTable3 操作数据库
var sql, sFile:string; db:TSQLiteDatabase;begin try sFile := G_AppPath + CH_IPC712Db; //if FileExist ...
- [PATCH] ARM: add dtbImage.<dt> and dtbuImage.<dt> rules
转载: http://permalink.gmane.org/gmane.linux.kbuild.devel/8755 This rules are useful for appended devi ...
- Microsoft office(1)分页符和分节符
Microsoft office下的页面布局中的分页符和分节符的区别: 分页符:标记一页的终止并开始下一页的点 分节符:插入分节符并在下一页开始新节 一般情况下,分节符在分页符外围,分节符一般是各种格 ...
- 深度增强学习--Deep Q Network
从这里开始换个游戏演示,cartpole游戏 Deep Q Network 实例代码 import sys import gym import pylab import random import n ...
- hive开窗函数over(partition by ......)用法
一.over(partition by ......)主要和聚合函数sum().count().avg()等结合使用,实现分组聚合的功能 示列:根据day_id日期和mac_id机器码进行聚合分组求每 ...
- Java NIO 选择器(Selector)的内部实现(poll epoll)(转)
转自:http://blog.csdn.net/hsuxu/article/details/9876983 之前强调这么多关于linux内核的poll及epoll,无非是想让大家先有个认识: Java ...
- 绝对让你理解Android中的Context
这个问题是StackOverFlow上面一个热门的问题What is Context in Android? 整理这篇文章的目的是Context确实是一个非常抽象的东西.我们在项目中随手都会用到它,但 ...
- [Angular] Control the dependency lookup with @Host, @Self, @SkipSelf and @Optional
Very differently to AngularJS (v1.x), Angular now has a hierarchical dependency injector. That allow ...