UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)
题意 :输入一棵二叉树,你的任务是按从上到下、从左到右的顺序输出各个结点的值。每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右)。在输入中,每个结点的左 括号和右括号之间没有空格,相邻结点之间用一个空格隔开。每棵树的输入用一对空括 号“()”结束(这对括号本身不代表一个结点),注意,如果从根到某个叶结点的路径上有的结点没有在输入中给出,或者给出超过一 次,应当输出not complete。结点个数不超过256。
分析 : 如果使用数组建树的话,256个结点看着不多,但是如果全部的节点都连成一条线的话数组是不足以存储的,所以采用指针链表的方式存储,动态建树。这道题还需要输出无法建造出树的情况,而一般的动态建树都是从根结点自上而下的建立,判断就变得些许麻烦了。如果从输入考虑的话,可以先按某种方式排序,使得输入和建树顺序是一致的,那么判断就方便了,这里紫书用了一个更简便的方法,直接在根节点的结构里面定义一个布尔数组来判断这个节点是否已经赋值,那么在建树的过程当中,不管当前这个点是否合法(其父节点已经存在),先全部动态分配内存建立起来,判断此树是否合法的工作只要在层序遍历时判断每个节点的布尔值即可。层序遍历则可以想象成BFS广搜在搜索解答树的过程,利用队列即可完成。
瞎 :
① strchr(char *, char) 即找到char *数组中第一次出现char的位置,类似于string::find_first_of()
②题目输入结束的条件是EOF,这时候不要轻易用while(1){...},超时也有可能是这样的输入导致,考虑将输入变成函数 bool read(){..}然后while(read()){...}来解决。
#include<bits/stdc++.h>
using namespace std;
struct Node
{
bool Have_val;
int val;
Node * Left, * Right;
Node():Have_val(false),Left(NULL),Right(NULL){}
};
];
Node * root;
bool Failed;
Node * newnode() { return new Node(); }
inline void Addnode(int num, char * s)
{
int len = strlen(s);
Node * u = root;
; i<len; i++){
if(s[i] == 'L'){
if(u->Left==NULL) u->Left = newnode();
u = u->Left;
}
else if(s[i] == 'R'){
if(u->Right==NULL) u->Right = newnode();
u = u->Right;
}
}
if(u->Have_val) Failed = true;
u->val = num;
u->Have_val = true;
}
]; ;
inline void bfs()
{
queue<Node *> q;
q.push(root);
while(!q.empty()){
Node * temp = q.front();
q.pop();
if(!temp->Have_val){
Failed = true;
return ;
}
ans[top++] = temp->val;
if(temp->Left!=NULL) q.push(temp->Left);
if(temp->Right!=NULL) q.push(temp->Right);
}
}
inline void destroy(Node * u)
{
if(u==NULL) return ;
destroy(u->Left);
destroy(u->Right);
delete u;
}
bool read()
{
Failed = false;
destroy(root);
root = newnode();
while(true){
) return false;
if(!strcmp(s, "()")) break;
int v;
sscanf(&s[], "%d", &v);
Addnode(v, strchr(s, );
}
return true;
}
int main(void)
{
while(read()){
top = ;
bfs();
if(Failed) puts("not complete");
else{
; i<top-; i++)
printf("%d ", ans[i]);
printf(]);
}
}
;
}
UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)的更多相关文章
- UVA.122 Trees on the level(二叉树 BFS)
UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...
- UVA 122 -- Trees on the level (二叉树 BFS)
Trees on the level UVA - 122 解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...
- LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...
- uva 122 trees on the level——yhx
题目如下:Given a sequence of binary trees, you are to write a program that prints a level-order traversa ...
- UVa 122 Trees on the level(二叉树层序遍历)
Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...
- UVA - 122 Trees on the level (二叉树的层次遍历)
题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...
- UVa 122 Trees on the level
题目的意思: 输入很多个节点,包括路径和数值,但是不一定这些全部可以构成一棵树,问题就是判断所给的能否构成一棵树,且没有多余. 网上其他大神已经给出了题目意思:比如我一直很喜欢的小白菜又菜的博客 说一 ...
- UVa 122 Trees on the level(链式二叉树的建立和层次遍历)
题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优 ...
- Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List<list>
问题描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...
随机推荐
- Buffer对象与JSON对象相互转换
> buffer=new Buffer('换汤不换药');<Buffer e6 88 91 e7 88 b1 e4 bd a0 ef bc 8c e7 89 a9 e7 90 86> ...
- <<C++ Primer>> 第 5 章 语句
术语表 第 5 章 语句 块(block): 包围在花括号内的由 0 条或多条语句组成的序列.块也是一条语句,所以只要是能使用语句的地方,就可以使用块. break语句(break statem ...
- hdu3829 二分匹配 最大独立集
Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Problem ...
- ubuntu 安装 Anaconda2和3的tips
Anaconda 2 3 安装tips 安装anaconda2 我们要下载Anaconda2-4.3.0-Linux-x86_64.sh安装文件 下载好之后,在文件路径下执行以下命令: bash An ...
- linux源码下载
概要:本文主要介绍ubuntu环境下,内核源码和命令源码的获取方式. 内核源码: 1.最简洁的方式,使用命令:apt-get source linux-$(uname -r).但配置的源服务器中不一定 ...
- mysql自增主键字段重排
不带外键模式的 mysql 自增主键字段重排 1.备份表结构 create table table_bak like table_name; 2.备份表数据 insert into table_bak ...
- Jade学习(二)之语法规则上
语法 ⚠️实例均结合node jade缩进代表层级 html <html></html> html <html> head <head> style & ...
- 解决 find: 路径必须在表达式之前:
通配符前面加\ 转义 或者 换个目录操作
- MySQL之常用SQL语句
1) 分表之后统计数据的总量 SELECT (a0.total + a1.total + a2.total + a3.total + a4.total + a5.total + a6.total + ...
- javascript 与node的 event-loop
参考:https://juejin.im/post/5c337ae06fb9a049bc4cd218 https://github.com/forthealllight/blog/issues/5 h ...