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

题意分析

给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete

代码总览

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <vector>
  7. #define nmax 10000
  8. using namespace std;
  9. bool failed;
  10. struct node{
  11. bool isvalue;
  12. node * left, *right;
  13. int val;
  14. node():isvalue(false),left(NULL),right(NULL){}
  15. };
  16. node* root;
  17. //node* roott;
  18. char s[nmax];
  19. node* newnode()
  20. {
  21. return new node();
  22. }
  23. bool addnode( int v, char * str)
  24. {
  25. int len = strlen(str);
  26. node*t = root;
  27. for(int i = 0; i<len; ++i){
  28. //if(len == 1)
  29. if(str[i] == 'L'){
  30. if(t->left == NULL) t->left = new node();
  31. t = t->left;
  32. }else if(str[i] == 'R'){
  33. if(t->right == NULL) t->right = new node();
  34. t = t->right;
  35. }
  36. }
  37. if(t->isvalue == true) failed = true;
  38. t->val = v;
  39. t->isvalue = true;
  40. return true;
  41. }
  42. bool read_input()
  43. {
  44. failed = false;
  45. root = newnode();
  46. for(;;){
  47. if(scanf("%s",s) !=1) return false;
  48. else{
  49. int v;
  50. if(!strcmp(s,"()")) break;
  51. sscanf(&s[1],"%d",&v);
  52. addnode(v,strchr(s,',')+1);
  53. }
  54. }
  55. return true;
  56. }
  57. bool bfs(vector<int> & ans)
  58. {
  59. queue<node*> q;
  60. ans.clear();
  61. q.push(root);
  62. while(!q.empty()){
  63. node* t = q.front();q.pop();
  64. if(t ->isvalue == false ) {failed = true; break;}
  65. ans.push_back(t->val);
  66. if(t){
  67. if(t->left)q.push(t->left);
  68. if(t->right)q.push(t->right);
  69. }
  70. }
  71. return true;
  72. }
  73. int main()
  74. {
  75. //freopen("in.txt","r",stdin);
  76. vector<int> ans;
  77. //roott = newnode();
  78. while(read_input()){
  79. bfs(ans);
  80. if(failed) printf("%s\n","not complete");
  81. else{
  82. bool flag = false;
  83. for(int i = 0; i<ans.size();++i){
  84. if(i == 0) printf("%d",ans[i]);
  85. else printf(" %d",ans[i]);
  86. }
  87. printf("\n");
  88. }
  89. }
  90. return 0;
  91. }

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. 容器云技术:容器化微服务,Istio占C位出道

    在精彩的软件容器世界中,当新项目涌现并解决你认为早已解决的问题时,这感觉就像地面在你的脚下不断地移动.在许多情况下,这些问题很久以前被解决,但现在的云原生架构正在推动着更大规模的应用程序部署,这就需要 ...

  2. Windows运行机理——主程序—WinMain

    Windows运行机理这系列文章都是来至于<零基础学Qt4编程>——吴迪,个人觉得写得很好,所以进行了搬运和个人加工 在windows 操作系统下,用C 或者C++来编写MS-DOS 应用 ...

  3. Linux命令应用大词典-第37章 Linux系统故障排错

    37.1 mkbootdisk:创建用于运行系统的独立启动软盘 37.2 chroot:切换根目录环境 37.3 badblocks:搜索设备的坏块 37.4 mkinitrd:创建要载入ramdis ...

  4. Java开发工程师(Web方向) - 01.Java Web开发入门 - 第2章.HTTP协议简介

    第2章--HTTP协议简介 HTTP协议简介 Abstract: HTTP协议的特性,HTTP请求/响应的过程,HTTP请求/响应的报文格式等知识,最后会演示如何通过Chrome提供的开发者工具,去跟 ...

  5. 【Python+OpenCV】人脸识别基于环境Windows+Python3 version_3(Anaconda3)+OpenCV3.4.3安装配置最新版安装配置教程

    注:本次安装因为我要安装的是win10(64bit)python3.7与OpenCV3.4.3教程(当下最新版,记录下时间2018-11-17),实际中这个教程的方法对于win10,32位又或是64位 ...

  6. 【input】输入框组件说明

    input输入框组件 原型: <input value="[String]" type="[text | number | idcard | digit]" ...

  7. Java 集合学习--HashMap

    一.HashMap 定义 HashMap 是一个基于散列表(哈希表)实现的键值对集合,每个元素都是key-value对,jdk1.8后,底层数据结构涉及到了数组.链表以及红黑树.目的进一步的优化Has ...

  8. jQuery的图片懒加载

    jQuery的图片懒加载 function imgLazyLoad(options) { var settings = { Id: $('img'), threshold: 100, effectsp ...

  9. JDK源码分析:Byte.java

    Byte是基本数据类型byte的包装类. 1)声明部分: public final class Byte extends Number implements Comparable<Byte> ...

  10. 最短路径算法(II)

    什么??你问我为什么不在一篇文章写完所有方法?? Hmm…其实我是想的,但是博皮的加载速度再带上文章超长图片超多的话… 可能这辈子都打不开了吧… 上接https://www.cnblogs.com/U ...