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 ...
随机推荐
- 在C#中快速查询文件
相信使用过Everything的人都对其超快的搜索速度印象非常深刻,它的主要原理是通过扫描NTFS磁盘的USN Journal读取的文件列表,而不是磁盘目录,由于USN Journal非常小,因此能实 ...
- 关于在.NET中 DAL+IDAL+Model+BLL+Web
其实三层架构是一个程序最基本的 在.Net开发中通常是多层开发比如说 BLL 就是business Logic laywer(业务逻辑层) 他只负责向数据提供者也就是DAL调用数据 然后传递给 ...
- ylbtech-dbs-m-YinTai(银泰网)
ylbtech-dbs:ylbtech-dbs-m-YinTai(银泰网) -- =============================================-- DatabaseNam ...
- 算法导论-求x的n次方
目录 1.分治求x的n次方思路 2.c++代码实现 内容 1.分治求x的n次方思路T(n)=Θ(lgn) 为了计算乘方数a^n,传统的做法(所谓的Naive algorithm)就是循环相乘n次,算法 ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- 持续集成之代码质量管理-Sonar
原文:http://blog.csdn.net/abcdocker/article/details/53840582 Sonar介绍 Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Son ...
- 【原】使用StarUML画用例图
在写一份升级方案的时候,发现文字描述半天,好多句子,依然不容易被人看明白,使用visio画了个流程图,后来觉得画个时序图是最清晰得了. 于是在找了一个工具: startUML,当然,做时序图,建模之类 ...
- Python学习笔记(三)多线程的使用
这节记录学习多线程的心得. Python提供了thread模块,不过该模块的缺点很多,例如无法方便的等待线程结束,所以我们使用更加高级的threading模块. threading模块的使用一 ...
- YII用户注冊和用户登录(五)之进行session和cookie分析 ,并在前后区分session和cookie
5 进行session和cookie分析 ,并在前后区分session和cookie: 记住登录状态 这样下次再登录站点的时候.就不用反复输入username和password. 是浏览器的cooki ...
- SSH——增删改的实现二
二.批量删除 逻辑删除取派员,将取派员的deltag改为“1” 1. 为“作废”按钮绑定事件 //批量删除取派员 function doDelete(){ //获得选中的行 var rows = $( ...