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的使用。的更多相关文章

  1. Trees on the level UVA - 122 (二叉树的层次遍历)

    题目链接:https://vjudge.net/problem/UVA-122 题目大意:输入一颗二叉树,你的任务是按从上到下,从左到右的顺序输出各个结点的值.每个结点都按照从根节点到它的移动序列给出 ...

  2. Trees on the level UVA - 122

    Trees are fundamental in many branches of computer science (Pun definitely intended). Current stateo ...

  3. 【紫书】Trees on the level UVA - 122 动态建树及bfs

    题意:给你一些字符串,代表某个值被插入树中的位置.让你输出层序遍历. 题解:动态建树. 由于输入复杂,将输入封装成read_input.注意输入函数返回的情况 再将申请新节点封装成newnode(). ...

  4. UVa 122 Trees on the level(链式二叉树的建立和层次遍历)

    题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优 ...

  5. UVA 122 -- Trees on the level (二叉树 BFS)

     Trees on the level UVA - 122  解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...

  6. UVA.122 Trees on the level(二叉树 BFS)

    UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...

  7. UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)

    题意  :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...

  8. 【例题 6-7 UVA - 122 】Trees on the level

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 二叉树的话,直接用数组存就好了. 写个bfs记录一下答案. [代码] #include <bits/stdc++.h> ...

  9. Trees on the level(指针法和非指针法构造二叉树)

    Trees on the level Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. xcode编译项目Permission denied错误

    打开终端,输入命令     sudo chmod -R 777 工作目录

  2. Centos7.3 bbc tools安装

    http://blog.csdn.net/orangleliu/article/details/54099528 更新到最新 CentOS 7.3 1611 yum update -y cat /et ...

  3. nodeJs常用API

    1.url (1)url.parse返回url对象的各种参数 url.parse(url,true/false,true/false);//默认url.parse(url,false,false); ...

  4. 你真的了解try{ return }finally{}中的return?(转载)

    发现一篇有意思的博文,分享一下 谁能给我我解释一下这段程序的结果为什么是:2.而不是:3 代码如下: class Test { public int aaa() { int x = 1; try { ...

  5. javascript字符串处理方法

    字符串处理方法 1.字符串合并操作:“ + ”2.parseInt() 将数字字符串转化为整数3.parseFloat() 将数字字符串转化为小数4.split() 把一个字符串分隔成字符串组成的数组 ...

  6. Yii2系列教程六:集成编辑器

    上一篇文章我们实现了简单的用户权限管理,至于更先进的RBAC,我后面会单独出一篇文章来说说.在这一篇文章当中,我主要想写的是在Yii2中集成一个编辑器,因为在我们的实际开发当中,一个简单的textar ...

  7. 为Linux上FireFox安装Flash插件

    废话少说,步骤如下: 1.点击网页上插件缺失处,根据提示下载tar.gz版本的插件,我下载的版本是install_flash_player_11_linux.i386.tar.gz,这个文件被下载到了 ...

  8. Unity3D使用TCP/IP协议,传递protocol buffer消息protobuf-net

    原文:http://my.oschina.net/faint/blog/296785 第一部分 dll 1 下面大多数内容,都是使用c#编译的dll来实现的. 2 编译为dll后,要拖放到unity3 ...

  9. OpenERP为form和tree视图同时指定view_id的方法

    Odoo,OpenERP中文网 / 2014-07-16 文所说的是关于OpenERP中同一个对象(同名继承)使用view_id来指定form和tree视图的方法,由于官方文档中Views and E ...

  10. libevent2源码分析之一:前言

    event的本质 libevent2中的event的本质是什么?只要是非同步阻塞的运行方式,肯定遵循事件的订阅-发布模型.通过event_new的函数原型可以理解,一个event即代表一次订阅,建立起 ...