UVa 122 Trees on the level(链式二叉树的建立和层次遍历)
题目链接:
https://cn.vjudge.net/problem/UVA-122
/*
问题
给出每个节点的权值和路线,输出该二叉树的层次遍历序列。 解题思路
根据输入构建链式二叉树,再用广度优先遍历保存权值最后输出。
*/
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn=;
bool failed; struct NODE{
bool have_value;
int v;
NODE *left,*right;
NODE() : have_value(false),left(NULL),right(NULL){};
};
NODE* newnode(){
return new NODE();
}
NODE* root; bool read_input();
void addnode(int v,char *s);
bool bfs(vector<int> &ans);
void remove_tree(NODE* u){
if(u == NULL) return;
remove_tree(u->left);
remove_tree(u->right);
delete u;
} int main()
{
//freopen("E:\\testin.txt","r",stdin);
vector<int> ans;
while(read_input()){
if(failed || !bfs(ans))
printf("not complete\n");
else{
int i;
for(i=;i<ans.size()-;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[i]);
}
}
return ;
} bool bfs(vector<int> &ans){
queue<NODE*> q;
ans.clear();
q.push(root); while(!q.empty()){
NODE* u =q.front(); q.pop();
if(!u->have_value) return false;
ans.push_back(u->v); if(u->left != NULL) q.push(u->left);
if(u->right != NULL) q.push(u->right);
}
return true;
}
void addnode(int v,char *s){
int len=strlen(s); NODE* u= root;
for(int i=;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_value) failed=true;
u->v= v;
u->have_value=true;
}
bool read_input(){
char s[maxn];
failed=false;
remove_tree(root);
root=newnode();
for(;;){
if(scanf("%s",s) != ) return false;
if(!strcmp(s,"()")) break;
int v;
sscanf(s+,"%d",&v);
addnode(v,strchr(s,',')+);
}
return true;
}
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 ...
- 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——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
题目的意思: 输入很多个节点,包括路径和数值,但是不一定这些全部可以构成一棵树,问题就是判断所给的能否构成一棵树,且没有多余. 网上其他大神已经给出了题目意思:比如我一直很喜欢的小白菜又菜的博客 说一 ...
- UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)
题意 :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...
- UVA - 122 Trees on the level (二叉树的层次遍历)
题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...
- 103 Binary Tree Zigzag Level Order Traversal 二叉树的锯齿形层次遍历
给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行).例如:给定二叉树 [3,9,20,null,null,15,7], 3 ...
- Leetcode103. Binary Tree Zigzag Level Order Traversal二叉树的锯齿形层次遍历
给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 / ...
随机推荐
- poj1573模拟
Robot Motion Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java ...
- 一个DELPHI操作USB摄像头类
最近在使用Usb摄像头做了个项目,其中写了一个操作usb摄像头类分享给大家 {*******************************************************} { } ...
- Xcode一些好用的插件,以及这些插件的管理器
最近从xcode6.4升级到xcode7,发现以前所有的插件都失效了,如果要安装,需要重新去一个个下载.安装,很麻烦. 于是,转来了这篇博文,亲自测试,发现很好用...... 地址:http://11 ...
- Golang Email
Backup Code I dont have chinese inputmethod now ( what the fuck about Linux KDE envirnment !) , so j ...
- collaborative filtering协同过滤
每次我想看电影的时候,都会去问我的朋友,小健.一般他推荐的电影,我都比较喜欢.显然不是所有人都有小健这样的能力.因为我碰巧和小健有类似的品味. 这个生活中的经验,实际上有着广泛的用途. 当系统需要为某 ...
- Martin Fowler 分层测试概念博文分享
在我们测试工作中,常常遇到这样的问题:开发与测试团队分属不同的不同(部门隔离.沟通不畅),质量职责划分不清(出现bug往往都是测试人员背锅),需求的不确定和易变性(需求不断变化导致代码不停更新.产品重 ...
- .net core 与ELK(2)安装Elasticsearch可视化工具
elasticsearch-head是els的界面插件,地址https://github.com/mobz/elasticsearch-head 1.进入github并下载 wget https:// ...
- Python文件读写及网站显示
一.关于文件读写的笔记 (一) 文件概述 文件是一个存储在辅助存储器上的数据序列,可以包含任何数据内容 文件都是按照2进制进行存储的,但在表现形式上有2种:文本文件和二进制文件. 1. 文本文件 文本 ...
- Vue.js之下拉列表及选中触发事件
老早就听说了Vue.js是多么的简单.易学.好用等等,然而我只是粗略的看了下文档,简单的敲了几个例子,仅此而已. 最近由于项目的需要,系统的看了下文档,也学到了一些东西. 废话不多说,这里要说的是下拉 ...
- mongodb postgresql mysql jsonb对比
mongodb pg mysql jsonb对比 http://erthalion.info/2017/12/21/advanced-json-benchmarks/ 使用禁用jsonb列的压缩 AL ...