题目给出一个后缀表达式,让你求从下往上的层次遍历。

思路:结构体建树,然后用数组进行BFS进行层次遍历,最后把数组倒着输出就行了。

uva过了,poj老是超时,郁闷。

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4.  
  5. const int maxn = 10001;
  6. char str[maxn];
  7. int p;
  8.  
  9. struct Node {
  10. char data;
  11. Node* l;
  12. Node* r;
  13. };
  14.  
  15. Node* build() {
  16. Node* u = (Node*) malloc (sizeof(Node*));
  17. u -> data = str[p];
  18. p--;
  19. if (str[p] >= 'A' && str[p] <= 'Z')
  20. u -> l = build();
  21. else
  22. u -> l -> data = str[p];
  23. p--;
  24. if (str[p] >= 'A' && str[p] <= 'Z')
  25. u -> r = build();
  26. else
  27. u -> r -> data = str[p];
  28. return u;
  29. }
  30.  
  31. int main() {
  32. int t;
  33. while (scanf("%d", &t) != EOF) {
  34. scanf("%s", str);
  35. p = strlen(str) - 1;
  36. Node* root = build();
  37. int rear = 0, front = 0;
  38. Node* q[maxn];
  39. q[rear++] = root;
  40. while (rear > front) {
  41. if (q[front]) {
  42. if (q[front] -> r)
  43. q[rear++] = q[front] -> r;
  44. if (q[front] -> l)
  45. q[rear++] = q[front] -> l;
  46. front++;
  47. }
  48. else
  49. front++;
  50. }
  51. for (int i = rear - 1; i >= 0; i--)
  52. printf("%c", q[i]->data);
  53. printf("\n");
  54. }//while
  55. }

uva 11234 Expressions 表达式 建树+BFS层次遍历的更多相关文章

  1. UVa 11234 Expressions (二叉树重建&由叶往根的层次遍历)

    画图出来后结果很明显 xyPzwIM abcABdefgCDEF sample output wzyxIPM gfCecbDdAaEBF * + - x y z w F B E a A d D b c ...

  2. leetcode@ [126] Word Ladder II (BFS + 层次遍历 + DFS)

    https://leetcode.com/problems/word-ladder-ii/ Given two words (beginWord and endWord), and a diction ...

  3. hdu 1622 Trees on the level(二叉树的层次遍历)

    题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...

  4. 树的层次遍历(Trees on the level,UVA 122)

    题目描述: 题目思路: 1.用结构链表来建树 2.用队列来实现层次遍历,当遍历到根节点时,将其子节点压入队列 #include <iostream> #include <cstdli ...

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

    题目链接:https://vjudge.net/problem/UVA-122 题目大意:输入一颗二叉树,你的任务是按从上到下,从左到右的顺序输出各个结点的值.每个结点都按照从根节点到它的移动序列给出 ...

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

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

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

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

  8. UVa 122 树的层次遍历

    题意: 给定一颗树, 按层次遍历输出. 分析: 用数组模拟二叉树, bfs即可实现层次遍历 #include <bits/stdc++.h> using namespace std; st ...

  9. PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)

    7-4 Cartesian Tree (30分)   A Cartesian tree is a binary tree constructed from a sequence of distinct ...

随机推荐

  1. JS制作的简单的三级及联

    前台: <form id="form1" runat="server"> <div> 省 <select id="Pro ...

  2. HDU4289Control(最大流)

    看了这道题,然后重新开始练习自己的刚敲不久的网络流,发现还是难以一遍敲得完整啊,,,,, 调了...遍,改了...遍,测了...遍,交了,,,遍,总算是A了,,不简单啊 然后试着用了其他两种算法EK和 ...

  3. POJ3080Blue Jeans(暴力)

    开始做字符串专题,地址 第一题水题,暴力就可以做 #include <map> #include <set> #include <stack> #include & ...

  4. MFC中消息响应机制

    由于视类窗口始终覆盖在框架类窗口之上,因此所有操作,包括鼠标单击.鼠标移动等操作都只能由视类窗口捕获.一个MFC消息响应函数在程序中有三处相关信息:函数原型.函数实现和以及用来关联消息和消息响应函数的 ...

  5. winform 发布应用程序 提示 “未能注册模块(程序路径)\ieframe.dll”

    程序安装的时候出现未能注册模块(程序路径)\ieframe.dll提示 这种情况的出现,是因为引用的shdocvw.dll,目前发现了一个折中的解决方法,在安装程序里面,可以看到ieframe.dll ...

  6. 一个优秀windows C++程序员的知识体系[转]

    转自:一个优秀windows C++程序员的知识体系 思考一个优秀windows C++ 程序员该有哪些知识,可最终发现什么知识都不能少, 看下图: 除了上面知识,程序员还要不断学习, 保持对新知识的 ...

  7. JS Flex交互:html嵌套Flex(swf)

    一.html页面嵌套Flex需要用到 swfobject.js swfobject的使用是非常简单的,只需要包含 swfobject.js这个js文件,然后在DOM中插入一些简单的JS代码,就能嵌入F ...

  8. Cocos2d-x利用CCHttpRequest获取网络图片并显示

    利用CCHttpRequest获取网上http地址的图片并缓存到本地生成CCSprite用于显示 //图片结构class imgstruct : public CCObject { public: i ...

  9. Android+Jquery Mobile学习系列(4)-页面跳转及参数传递

    关于页面转场,这个必须得专门列出来说明一下,因为Jquery Mobile与普通的Web发开有一些区别,这个对于新手如果不了解的话,就会钻到死胡同.撸主前段时间就是很急躁地上手开发程序,结果在页面转场 ...

  10. php计算脚本执行时间

    利用PHP的microtime实现 function getCurrentTime () { list ($msec, $sec) = explode(" ", microtime ...