03-树3. Tree Traversals Again (25)
2015-07-22 09:58
585 查看
03-树3. Tree Traversals Again (25)
时间限制200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop();
push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.
![](http://www.patest.cn/upload/bs_n9mde9jcnyj.jpg)
Figure 1
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in
the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.
Output Specification:
For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
6 Push 1 Push 2 Push 3 Pop Pop Push 4 Pop Pop Push 5 Push 6 Pop Pop
Sample Output:
3 4 2 6 5 1
#include <stdio.h> struct Node{ int tag; //标记节点是第几次进栈 int num; }; //先序遍历对应进栈顺序,中序遍历对应出栈顺序; //后序遍历与中序遍历不同的是节点出栈后要马上再入栈(tag做第二次入栈标记),等右儿子遍历完后再出栈; //具体实现上,每次中序遍历的pop时,如果栈顶是标记过的节点(tag=2),循环弹出;如果没有标记过(tag=1),做标记,即弹出再压栈) //栈顶tag=2的节点对应中序遍历中已弹出的节点;循环弹出后碰到的第一个tag=1的节点才对应中序遍历当前pop的节点 int main() { freopen("test.txt", "r", stdin); int n; scanf("%d", &n); int flag = 0; struct Node stack[30]; int size = 0; //栈元素大小,指向栈顶的下一个位置 for (int i = 0; i < 2 * n; ++i) { char s[10]; scanf("%s", s); if (s[1] == 'u') { //push scanf("%d", &stack[size].num); //入栈 stack[size].tag = 1; //标记第一次入栈 ++size; } else { //pop while (size > 0 && stack[size - 1].tag == 2) { //循环弹出栈顶tag=2的节点 if (flag) printf(" "); flag = 1; printf("%d", stack[--size].num); } if (size > 0) //将中序遍历中应该要弹出的节点弹出再压栈,做标记即可 stack[size - 1].tag = 2; } } while (size) { //将栈中剩余节点依次弹出 if (flag) { printf(" "); } flag = 1; printf("%d", stack[--size].num); } return 0; }
题目链接:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%913
相关文章推荐
- 【线段树】2015 Multi-University Training Contest 1 1002
- 【数论】2015 Multi-University Training Contest 1 1001
- hdu 5291 Candy Distribution 2015 Multi-University Training Contest 1
- grails domain创建相关问题
- 深入解析alloc/retain/release/dealloc实现
- hdu1151 二分图(无回路有向图)的最小路径覆盖 Air Raid
- ubuntu 10.10 drcom成功上网 好不容易。附:_recv_dialog_packet(PKT_CHALLENGE) failed解决之道
- URL validation failed. The error could have been caused through the use of the browser's navigation
- bailian.openjudge.cn2705
- VT.Essay.1:Try again. Fail again. Fail better.
- Russell's Paradox: Here's Why Maths Can't Have A Set Of Everything
- HDU 5289 Assignment (2015 Multi-University Training Contest 1)
- HDU5294 Tricks Device(最大流+SPFA) 2015 Multi-University Training Contest 1
- Copy,mutableCopy和retain的区别的小理解
- 2015 Multi-University Training Contest 1 题解 BY FZUw
- service是否在main thread中执行, service里面是否能执行耗时的操作?
- MFC学习笔记.day01-WINMAIN,MSG,WNDCLASS,ID-HANDLE-HWND,图标光标
- hdu 2473 Junk-Mail Filter(并查集(虚拟父节点))
- 2015 Multi-University Training Contest 1 Assignment
- SDNU 1143.Ivan comes again!【山东省第一届ACM】【7月21】