UVA.122 Trees on the level(二叉树 BFS)
UVA.122 Trees on the level(二叉树 BFS)
题意分析
给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete
代码总览
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#define nmax 10000
using namespace std;
bool failed;
struct node{
bool isvalue;
node * left, *right;
int val;
node():isvalue(false),left(NULL),right(NULL){}
};
node* root;
//node* roott;
char s[nmax];
node* newnode()
{
return new node();
}
bool addnode( int v, char * str)
{
int len = strlen(str);
node*t = root;
for(int i = 0; i<len; ++i){
//if(len == 1)
if(str[i] == 'L'){
if(t->left == NULL) t->left = new node();
t = t->left;
}else if(str[i] == 'R'){
if(t->right == NULL) t->right = new node();
t = t->right;
}
}
if(t->isvalue == true) failed = true;
t->val = v;
t->isvalue = true;
return true;
}
bool read_input()
{
failed = false;
root = newnode();
for(;;){
if(scanf("%s",s) !=1) return false;
else{
int v;
if(!strcmp(s,"()")) break;
sscanf(&s[1],"%d",&v);
addnode(v,strchr(s,',')+1);
}
}
return true;
}
bool bfs(vector<int> & ans)
{
queue<node*> q;
ans.clear();
q.push(root);
while(!q.empty()){
node* t = q.front();q.pop();
if(t ->isvalue == false ) {failed = true; break;}
ans.push_back(t->val);
if(t){
if(t->left)q.push(t->left);
if(t->right)q.push(t->right);
}
}
return true;
}
int main()
{
//freopen("in.txt","r",stdin);
vector<int> ans;
//roott = newnode();
while(read_input()){
bfs(ans);
if(failed) printf("%s\n","not complete");
else{
bool flag = false;
for(int i = 0; i<ans.size();++i){
if(i == 0) printf("%d",ans[i]);
else printf(" %d",ans[i]);
}
printf("\n");
}
}
return 0;
}
UVA.122 Trees on the level(二叉树 BFS)的更多相关文章
- 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 (二叉树 BFS)
Trees on the level UVA - 122 解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...
- UVa 122 Trees on the level(链式二叉树的建立和层次遍历)
题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优 ...
- UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)
题意 :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...
- UVA - 122 Trees on the level (二叉树的层次遍历)
题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...
- 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 Tree on the level)
内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时, ...
- Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...
随机推荐
- 用最简单的MVC模式输出内容
MVC是模型(model)-视图(view)-控制器(controller)的缩写,它的作用是使代码分离,可维护性高.重用性高 编写Model层: <?php class model{ publ ...
- PyCharm添加Selenium与Appium类库
PyCharm添加Selenium与Appium依赖, 不需要用pip去安装!
- Linux命令应用大词典-第4章 目录和文件操作
4.1 pwd:显示(打印)当前工作目录路径 4.2 cd:更改工作目录路径 4.3 ls: 列出目录和文件信息: 4.4 dir:列出目录或文件信息: 4.5 dirs:显示目录列表: 4.6 to ...
- django 与 flask里面从已有数据库表中反向生成models
django: 配置好数据库连接 python manage.py inspectdb > models.py 即可反向生成orm使用的models, 注意: > 后面可以定义为指定路 ...
- Ducci序列 (Ducci Sequence,ACM/ICPC Seoul 2009,UVa1594)
题目描述: 题目思路: 直接模拟 #include<stdio.h> #include<string.h> #define maxn 105 int less(const ch ...
- UVa 340 - Master-Mind Hints 解题报告 - C语言
1.题目大意 比较给定序列和用户猜想的序列,统计有多少数字位置正确(x),有多少数字在两个序列中都出现过(y)但位置不对. 2.思路 这题自己思考的思路跟书上给的思路差不多.第一个小问题——位置正确的 ...
- 1.EOS源码编译运行
目前网络上都是针对老版EOS2.0源码编译的文章,我在mac上参考这些文章编译,最后发现根本就不对,最新版本只需一条命令(./eosio_build.sh,依赖库会自动安装的)即可.我根据这些文章手动 ...
- 正确使用memset
今天做了一道素数打表的题我在使用一个数组记录是否为素数的时候使用了memset,将数组里面的数都清为1,代表是素数,不是素数,就改成0,我在判断这一个数是否为素数是依据也是是0还是1,结果一直存在问题 ...
- POJ 1995 (快速幂)
这道题普通做法会发生溢出且会超时,应当用快速幂来求解. 快速幂讲解 #include <cstdio> #include <cmath> using namespace std ...
- apache 多端口访问 配置
使用本地ip:端口号,或者修改hosts文件+域名的方法来进行本地多站点web调试. 注意这里是用apache 不是iis 1: 安装好AppServ2.5.9软件,官网是:[url]http ...