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)的更多相关文章

  1. UVa 122 Trees on the level(二叉树层序遍历)

    Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...

  2. UVA 122 -- Trees on the level (二叉树 BFS)

     Trees on the level UVA - 122  解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...

  3. UVa 122 Trees on the level(链式二叉树的建立和层次遍历)

    题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优 ...

  4. UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)

    题意  :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...

  5. UVA - 122 Trees on the level (二叉树的层次遍历)

    题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了 ...

  6. 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 ...

  7. UVa 122 Trees on the level

    题目的意思: 输入很多个节点,包括路径和数值,但是不一定这些全部可以构成一棵树,问题就是判断所给的能否构成一棵树,且没有多余. 网上其他大神已经给出了题目意思:比如我一直很喜欢的小白菜又菜的博客 说一 ...

  8. 内存池技术(UVa 122 Tree on the level)

    内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时, ...

  9. Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。

    Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...

随机推荐

  1. 微信小程序学习笔记(1)-微信小程序样式设置逻辑

    1.微信小程序的样式设置统一在每一页的.wxss的样式文件中,所有的样式设置代码统一写入这个文件中: 2.样式主要是通过.wxml里面控件的“class”属性来调用,此处调用会有几个细节要注意: 1) ...

  2. 游戏人工智能 读书笔记 (四) AI算法简介——Ad-Hoc 行为编程

    本文内容包含以下章节: Chapter 2 AI Methods Chapter 2.1 General Notes 本书英文版: Artificial Intelligence and Games ...

  3. webpack中Development和Production模式的区分打包

    当我们在开发一个项目的时候,我们一般用development这个环境进行项目的开发,在这个环境下,webpack使用dev-server,帮我们启用一个服务器,然后这个服务器里面还集成了一些,比如hm ...

  4. JVM学习--jvm监控和故障处理工具

    java虚拟机性能监控常用命令 Sun JDK监控和故障处理命令有jps.jstat.jinfo.jmap.jhat.jstack . 1.jps jps:JVM Process Status Too ...

  5. leetcode-汉明距离

    汉明距离 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...

  6. 【shell 练习5】编写简单的多级菜单

    一.简单的多级菜单 [root@web129 ~]# cat menu.sh #!/bin/bash #shell菜单演示 function menu() { echo -e `date` cat & ...

  7. poj 3468 (区间修改 区间查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions:147133   ...

  8. CVPR-2018 那些有趣的新想法

    Taylor Guo @ Shanghai - 2018.10.18 缘起 还有什么比顶级会议更适合寻找有趣新想法的地方吗?我们从CVPR 2018 计算机视觉和模式识别的顶级会议中发现了很多有趣的东 ...

  9. php 面试题

    1.通过哪一个函数,可以把错误转换为异常处理? A:set_error_handlerB:error_reportingC:error2exceptionD:catch 正确答案:A 答案分析:set ...

  10. Coprime Sequence(前后缀GCD)

    Description Do you know what is called ``Coprime Sequence''? That is a sequence consists of $n$ posi ...