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. Scala零基础教学【102-111】Akka 实战-深入解析

    第102讲:通过案例解析Akka中的Actor运行机制以及Actor的生命周期 Actor是构建akka程序的核心基石,akka中actor提供了构建可伸缩的,容错的,分布式的应用程序的基本抽象, a ...

  2. WPF附加属性的Set函数不调用的问题

    今天写程序的时候用到了附加属性,我是用VS内置的propa的代码段来实现的,代码如下: class Attach    {        public static bool GetIsEnabled ...

  3. Oracle Service Bus Socket Adapter调整的参数

    之前在一个客户中做压力测试时候Oracle Service Bus性能大概达到900tps左右,和客户期望的1600tps有很大差距. 在研究了Socket Adapter的工作原理之后,判断可能是O ...

  4. Microsoft SQL Server 2008 R2

    1概述 Microsoft SQL Server 2008 R2 提供完整的企业级技术与工具,帮助您以最低的总拥有成本获得最有价值的信息.您可以充分享受高性能,高可用性,高安全性,使用更多的高效管理与 ...

  5. Linux的PCI驱动分析

    1. 关键数据结构 PCI设备上有三种地址空间:PCI的I/O空间.PCI的存储空间和PCI的配置空间.CPU可以访问PCI设备上的所有地址空间,其中I/O空间和存储空间提供给设备驱动程序使用,而配置 ...

  6. vulkan

    https://gfxbench.com/device.jsp?benchmark=gfx40&os=Android&api=gl&D=Asus+ZenFone+4+%28Ad ...

  7. Java源码阅读PriorityQueue

    1类签名与简介 public class PriorityQueue<E> extends AbstractQueue<E> implements java.io.Serial ...

  8. 【OpenStack 虚拟机初始化user-data & Cloud-init】

    示例: import httplib import json import base64 tenant_id='xxx' token='xxx' compute_host="xxx" ...

  9. 利用velocity.js将svg动起来

    关于velocity.js Velocity.js是一款jquery动画引擎插件,它拥有与jquery中的$.animate()相同的API,还打包了颜色动画,转换,循环,easing效果,类动画.滚 ...

  10. EffectManager

    using UnityEngine; using System.Collections; public class EffectManager : MonoBehaviour { public Ani ...