题目:

给出一棵树的表示,判断这棵树是否输入正确,如果正确就按层次遍历输出所有的结点,错误的话就输出not complete。

思路:

根据字符串中树的路径先将树建起来,在增加结点和层次遍历树的时候判断这棵树是不是输入正确。

错误的两种情况:

1.同一个结点被输入的两次

2.这个结点的孩子有值,但这个结点没有被输入值。

判断方法:

根据字符串中给出的路径,将整棵树先建起来,每个结点中设一个是否被访问过的标志vis

增加结点时如果这个结点的vis已经为true则为错误的情况1。

遍历树的时候,如果这个结点已经建起来了,但是value没有被赋值,则为错误情况2。

最后利用队列对树进行层次遍历,并输出。

代码:

C++

  1. #include <bits/stdc++.h>
  2. #define inf 0x3f3f3f3f
  3. #define MAX 1e9;
  4. #define FRE() freopen("in.txt","r",stdin)
  5. #define FRO() freopen("out.txt","w",stdout)
  6. using namespace std;
  7. typedef long long ll;
  8. const int maxn = ;
  9. struct Node{
  10. Node* rt;
  11. Node* lt;
  12. int value;
  13. bool vis;
  14. Node(){
  15. rt = lt = NULL;
  16. value = ;
  17. vis = false;
  18. }
  19. };
  20. bool fail = false;
  21. Node* root = new Node();
  22.  
  23. void removeTree(Node* u){//释放树占的内存空间
  24. if(u==NULL) {return;}
  25. removeTree(u->lt);
  26. removeTree(u->rt);
  27. delete u;
  28. }
  29.  
  30. int makeNum(string str){//提取字符串中的数字
  31. int ans = ;
  32. for(int i = ; str[i]!=','; i++){
  33. ans = ans* + str[i]-'';
  34. }
  35. return ans;
  36. }
  37.  
  38. void addNode(Node* root,int value,string str){
  39. Node* p = root;
  40. for(int i = ; i<str.length(); i++){//忽略字符串中不是L和R的字符
  41. if(str[i]=='L'){//L则向左建树
  42. if(p->lt==NULL){
  43. p->lt = new Node();
  44. }
  45. p = p->lt;
  46. }else if(str[i]=='R'){//R则向右建树
  47. if(p->rt==NULL){
  48. p->rt = new Node();
  49. }
  50. p = p->rt;
  51. }
  52. }
  53. if(p->vis){//如果这个结点已经被赋值,则是错误输入情况1
  54. fail = true;
  55. }
  56. p->value = value;//赋值
  57. p->vis = true;//标记已经访问过
  58. }
  59.  
  60. void printTree(Node* root){
  61. vector<int> ans;
  62. queue<Node*> que;
  63. que.push(root);
  64. while(!que.empty()){
  65. Node* temp = que.front();
  66. que.pop();
  67. if(!temp->vis) fail = true;//如果这个结点被建出来,但是没有被赋值,则是错误情况2
  68. ans.push_back(temp->value);
  69. if(temp->lt) que.push(temp->lt);
  70. if(temp->rt) que.push(temp->rt);
  71. }
  72. if(fail){
  73. cout<<"not complete"<<endl;
  74. }else{
  75. cout<<ans[];
  76. for(int i = ; i<ans.size(); i++){
  77. cout<<" "<<ans[i];
  78. }
  79. cout<<endl;
  80. }
  81. }
  82.  
  83. int main(){
  84. string str;
  85. while(cin>>str){
  86. if(str=="()"){//这棵树输入结束,打印树
  87. printTree(root);
  88. removeTree(root);
  89. root = new Node();
  90. fail = false;
  91. }else{
  92. int value = makeNum(str);
  93. //cout<<str<<" "<<value<<endl;
  94. addNode(root, value, str);
  95. }
  96. }
  97. return ;
  98. }

Java

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class Node{
  5. Node rt;
  6. Node lt;
  7. int value;
  8. boolean vis;
  9. public Node() {
  10. rt = null;lt = null;
  11. value = 0;vis = false;
  12. }
  13. }
  14.  
  15. public class Main {
  16. public static boolean fail = false;
  17. public static void printTree(Node root) {
  18. Queue<Node> que = new LinkedList<>();
  19. LinkedList<Integer> list = new LinkedList<>();
  20. if(root==null) {
  21. fail = true;
  22. }else {
  23. que.offer(root);
  24. while(!que.isEmpty()) {
  25. Node temp = que.peek();que.poll();
  26. if(!temp.vis) {fail = true;}
  27. list.add(temp.value);
  28. if(temp.lt!=null)
  29. que.offer(temp.lt);
  30. if(temp.rt!=null)
  31. que.offer(temp.rt);
  32. }
  33. }
  34. if(!fail) {
  35. System.out.print(list.get(0));
  36. for(int i = 1; i<list.size(); i++) {
  37. System.out.print(" "+list.get(i));
  38. }
  39. System.out.println();
  40. }else {
  41. System.out.println("not complete");
  42. }
  43. }
  44.  
  45. public static void addNode(Node root, int value, String str) {
  46. Node p = root;
  47. for(int i = 0; i<str.length(); i++) {
  48. if(str.charAt(i)=='L') {
  49. if(p.lt==null) {
  50. p.lt = new Node();
  51. }
  52. p = p.lt;
  53. }else if(str.charAt(i)=='R') {
  54. if(p.rt==null) {
  55. p.rt = new Node();
  56. }
  57. p = p.rt;
  58. }
  59. }
  60. if(p.vis) {
  61. fail = true;
  62. }
  63. p.value = value;
  64. p.vis = true;
  65. }
  66.  
  67. public static int makeNum(String str) {
  68. int num = 0;
  69. for(int i = 1; str.charAt(i)!=','; i++) {
  70. num = num*10 + str.charAt(i)-'0';
  71. }
  72. return num;
  73. }
  74.  
  75. public static void main(String[] args) {
  76. Node root = new Node();
  77. Scanner scan = new Scanner(System.in);
  78. String str=null;
  79. while(scan.hasNext()) {
  80. str = scan.next();
  81. if(str.equals("()")) {
  82. printTree(root);
  83. root = new Node();
  84. fail = false;
  85. }else {
  86. int value = makeNum(str);
  87. addNode(root, value, str);
  88. }
  89. }
  90. scan.close();
  91. }
  92. }

用java做这个题的时候,利用split方法来处理字符串,结果一直是RE,找不出来了,就换了中方法(如上述代码中的)来实现,AC。

uva-122 Trees on the level(树的遍历)的更多相关文章

  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)

    UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...

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

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

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

  5. UVa 122 Trees on the level

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

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

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

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

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

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

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

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

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

  10. 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. 在linux上处理base64加密和解密

    http://snailwarrior.blog.51cto.com/680306/142472/ 2.从标准输入读取文件内容,base64编码并打印到标准输出 [root@localhost tes ...

  2. window环境下在anconda中安装opencv

    今日学习CNN神经网络,在用keras框架下搭建一个简单的模型的时候需要import cv2,我尝试了一下几种方法: 1. 在prompt输入 pip intall opencv-python 出现如 ...

  3. 3-1 todolist功能开发

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Tomcat启动Web.xml引用其它XML配置报FileNotFound异常解决方案

    如果使用JEECG框架进行Tomcat启动时,如果web.xml引用了其他xml文件,需要在tomcat文件夹里的config文件夹里的context.xml文件里的Context标签里配置xmlBl ...

  5. bzoj题目大体分类

    http://m.blog.csdn.net/article/details?id=51387623

  6. ngCordova插件说明

    转载自 http://my.oschina.net/u/1416844/blog/495026 参 考http://blog.csdn.net/superjunjin/article/details/ ...

  7. robotframework - Edit编辑器

    1.测试项目&套件 提供的Edit编辑器 2.在 Edit 标签页中主要分:加载外部文件.定义内部变量.定义元数据等三个部分. (1):加载外部文件Add Library:加载测试库,主要是[ ...

  8. 1051 复数乘法(C#)

    一.题目内容如下: 复数可以写成 ( 的常规形式,其中 A 是实部,B 是虚部,i 是虚数单位,满足 1:也可以写成极坐标下的指数形式 (,其中 R 是复数模,P 是辐角,i 是虚数单位,其等价于三角 ...

  9. java entity

    对java实体类的众多理解: A .就是属性类,通常定义在model层里面 B. 一般的实体类对应一个数据表,其中的属性对应数据表中的字段.好处:1.对对象实体的封装,体现OO思想.2.属性可以对字段 ...

  10. ACM_数数有多少(第二类Stirling数-递推dp)

    数数有多少 Time Limit: 2000/1000ms (Java/Others) Problem Description: 小财最近新开了一家公司,招了n个员工,但是因为资金问题,办公楼只有m间 ...