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. uvaoj1339 - Ancient Cipher(思维题,排序,字符串加密)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. hdu1175连连看(dfs+细节)

    连连看 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  3. Appium_Python_API说明

    Appium_Python_API 1.contexts contexts(self): Returns the contexts within the current session. 返回当前会话 ...

  4. jmeter3.0 java请求

    1.java请求说明 需要压测某些java方法或一些请求需要通过编写代码实现 1.1.依赖jar包: jmeter下/lib/ext中的ApacheJMeter_java.jar(必须).Apache ...

  5. ant-design学习准备_1

    在学习ant-desin过程中,发现很多知识都不清楚,从现在开始,每天将自己学习到的知识进行一个总结记录,前端大佬勿扰勿喷.先介绍几个基础概念和一些常用命令: 1.什么是脚手架 我们经常在各个博客论坛 ...

  6. CodeForces 838B Diverging Directions 兼【20180808模拟测试】t3

    描述 给你一个图,一共有 N 个点,2*N-2 条有向边. 边目录按两部分给出 1. 开始的 n-1 条边描述了一颗以 1 号点为根的生成树,即每个点都可以由 1 号点到达. 2. 接下来的 N-1 ...

  7. iconFont 阿里巴巴矢量图标使用方法

    挑选图标的过程(共6步) 进入网站:Iconfont网址:http://www.iconfont.cn 点击网站上方的“官方图标库”,选择自己喜欢的图标.在这里我选择天猫的图标库. 选择好自己喜欢的图 ...

  8. Phpcms V9导航循环下拉菜单的调用技巧

    这个方法基于PC V9官方模版中的调用方法,然后利用后台的“Phpcms V9菜单是否显示设置”控制菜单是否显示出来. 先看看最后的效果: 调用方法: <div id="navbar& ...

  9. M2功能规格说明书

    1.目的: 这篇随笔是简述我们团队所做的工程所能实现的功能及方便用户的使用. 2.假定和约束: 我们先限定为本地连接数据库进行各种操作的实现.用户电脑中需要有FLASH工具及快播插件.其他只需要了解基 ...

  10. block知识总结

    一.block在内存中存在的形式 1.当把block句法写在函数或者方法外面时,系统会在静态数据区分配一块内存区域给block对象.这片区域在程序执行期会一直存在. 2.当block句法写在函数或者方 ...