Problem Description
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
Pop
Sample Output
3 4 2 6 5 1
Solution
- 题意理解:根据非递归中序遍历的出入栈顺序求出后序遍历的顺序。
- 需要明白的是:出入栈的顺序中,入栈的顺序就是先序遍历的顺序;出栈的顺序就是中序遍历的顺序。
- 已知先序遍历和中序遍历可以还原出树,但在这里我们不需要还原,直接在数组中运用递归方法算出后序遍历的顺序。先序遍历和中序遍历的顺序都存在数组中。
- 在输入函数中用数组和数组指针设计一个小型栈,保存先序遍历 pre 与中序遍历 in
- 递归函数:
- 先序遍历的第一个结点为根结点,在后序遍历中是最后一个访问的。所以先从 pre 队首元素出队,作为 post 的最后一个元素
- root = pre[preL];
- post[postL + n - 1] = root;
- 确定根结点后,可以在中序遍历中找到根结点的位置,左右两边即为左右子树的中序遍历顺序。
- for (i = 0; i < n; i++) { if (in[inL + i] == root) break;}
- L = i; R = n - L - 1;
- 这里要注意 i 的含义是左子树结点个数,所以在循环中的判断应该是从 in 最左端往右数第 i 个元素是否等于 root 值
- 确定好左右子树规模后可以进行递归
- Solve(preL + 1, inL, postL, L);
- 左子树:新 preL 右移一位;新 inL 保持不变;新 postL 保持不变;左子树规模为 L
- Solve(preL + L + 1, inL + L + 1, postL + L, R);
- 右子树:新 preL 在右移 L + 1 位处;新 inL 在右移 L + 1 位处,因为中间隔了一个根结点;新 postL 在右移 L 位处;右子树规模为 R
- Solve(preL + 1, inL, postL, L);
- 递归终止条件:
- 子树规模为 0 (边缘情况)直接返回
- 子树规模为 1 的时候 pre 就是 post ,赋值后返回
- 先序遍历的第一个结点为根结点,在后序遍历中是最后一个访问的。所以先从 pre 队首元素出队,作为 post 的最后一个元素
#include <stdio.h>
#include <stdlib.h>
int n, pre[30], in[30], post[30];
void Input()
{
char c1, c2;
int i, num, p, q;
int top, stack[30];
top = 0;
scanf("%d", &n);
getchar();
p = 0;
q = 0;
for (i = 0; i < 2 * n; i++) {
scanf("%c%c", &c1, &c2);
if (c2 == 'u') {
scanf("%c%c %d", &c1, &c2, &num);
getchar();
pre[p++] = num;
stack[top++] = num;
} else {
scanf("%c", &c1);
getchar();
in[q++] = stack[--top];
}
}
}
void Solve(int preL, int inL, int postL, int n)
{
int i, root, L, R;
if (n == 0) return;
if (n == 1) {
post[postL] = pre[preL];
return;
}
root = pre[preL];
post[postL + n - 1] = root;
for (i = 0; i < n; i++) {
if (in[inL + i] == root) break;
}
L = i;
R = n - L - 1;
Solve(preL + 1, inL, postL, L);
Solve(preL + L + 1, inL + L + 1, postL + L, R);
}
void Print()
{
int i;
for (i = 0; i < n; i++) {
if (i) printf(" %d", post[i]);
else printf("%d", post[i]);
}
}
int main()
{
Input();
Solve(0, 0, 0, n);
Print();
return 0;
}