03-树3. Tree Traversals Again (25)
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.

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 PopSample 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