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- ...
随机推荐
- 容器云技术:容器化微服务,Istio占C位出道
在精彩的软件容器世界中,当新项目涌现并解决你认为早已解决的问题时,这感觉就像地面在你的脚下不断地移动.在许多情况下,这些问题很久以前被解决,但现在的云原生架构正在推动着更大规模的应用程序部署,这就需要 ...
- Windows运行机理——主程序—WinMain
Windows运行机理这系列文章都是来至于<零基础学Qt4编程>——吴迪,个人觉得写得很好,所以进行了搬运和个人加工 在windows 操作系统下,用C 或者C++来编写MS-DOS 应用 ...
- Linux命令应用大词典-第37章 Linux系统故障排错
37.1 mkbootdisk:创建用于运行系统的独立启动软盘 37.2 chroot:切换根目录环境 37.3 badblocks:搜索设备的坏块 37.4 mkinitrd:创建要载入ramdis ...
- Java开发工程师(Web方向) - 01.Java Web开发入门 - 第2章.HTTP协议简介
第2章--HTTP协议简介 HTTP协议简介 Abstract: HTTP协议的特性,HTTP请求/响应的过程,HTTP请求/响应的报文格式等知识,最后会演示如何通过Chrome提供的开发者工具,去跟 ...
- 【Python+OpenCV】人脸识别基于环境Windows+Python3 version_3(Anaconda3)+OpenCV3.4.3安装配置最新版安装配置教程
注:本次安装因为我要安装的是win10(64bit)python3.7与OpenCV3.4.3教程(当下最新版,记录下时间2018-11-17),实际中这个教程的方法对于win10,32位又或是64位 ...
- 【input】输入框组件说明
input输入框组件 原型: <input value="[String]" type="[text | number | idcard | digit]" ...
- Java 集合学习--HashMap
一.HashMap 定义 HashMap 是一个基于散列表(哈希表)实现的键值对集合,每个元素都是key-value对,jdk1.8后,底层数据结构涉及到了数组.链表以及红黑树.目的进一步的优化Has ...
- jQuery的图片懒加载
jQuery的图片懒加载 function imgLazyLoad(options) { var settings = { Id: $('img'), threshold: 100, effectsp ...
- JDK源码分析:Byte.java
Byte是基本数据类型byte的包装类. 1)声明部分: public final class Byte extends Number implements Comparable<Byte> ...
- 最短路径算法(II)
什么??你问我为什么不在一篇文章写完所有方法?? Hmm…其实我是想的,但是博皮的加载速度再带上文章超长图片超多的话… 可能这辈子都打不开了吧… 上接https://www.cnblogs.com/U ...