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
看完题就觉得这道题可以不用根据输入去建个树然后再后续遍历,但是我一开始就很在意题里说的一句话“a unique binary tree can be generated from this sequence of operations”。不是说要一个中序加上一个先序或者后序才能唯一确定一个树吗?但给出的操作只告诉了我们中序啊?看来输入一定不只有中序的信息,想了半天才想到输入的push的顺序不就是先序序列吗,于是根据这两个序列,后序序列就出来了,我用的是递归的方法做的。
#include <stdio.h> #include <stdlib.h> int n; void genPostOder (int preOrder[], int inOrder[], int size) { if(size == 1 && preOrder[0] == inOrder[0]) printf("%d ", preOrder[0]); else if(size > 1) { int length1, length2; int root = preOrder[0]; int i; for(i = 0; i < size; i++) if(inOrder[i] == root) break; int rootp = i; length1 = rootp; length2 = size - rootp - 1; int *inOrder1 = (int *) malloc (length1 * sizeof(int)); int *inOrder2 = (int *) malloc (length2 * sizeof(int)); int l1=0, l2=0; for(i = 0; i < size; i++) { if(i < rootp) inOrder1[l1++] = inOrder[i]; else if(i > rootp) inOrder2[l2++] = inOrder[i]; } int *preOrder1 = (int *) malloc (length1 * sizeof(int)); int *preOrder2 = (int *) malloc (length2 * sizeof(int)); l1=0, l2=0; for(i = 1; i < size; i++) { if(i <= length1) preOrder1[l1++] = preOrder[i]; else preOrder2[l2++] = preOrder[i]; } genPostOder(preOrder1, inOrder1, length1); genPostOder(preOrder2, inOrder2, length2); if(size < n) printf("%d ", root); } } void push (int stack[], int *length, int num) { stack[(*length)++] = num; } int pop (int stack[], int *length) { return stack[--(*length)]; } int main() { char ch; int node; scanf("%d", &n); getchar(); int *inOrder = (int *) malloc (n * sizeof(int)); int *preOrder = (int *) malloc (n * sizeof(int)); int *stack = (int *) malloc (n * sizeof(int)); int length=0, inl=0, prel=0; int i; for(i = 0; i < 2*n; i++) { scanf("P%c", &ch); if(ch == 'u') { scanf("sh %d", &node); getchar(); push(stack, &length, node); preOrder[prel++] = node; } else if(ch == 'o') { scanf("p"); getchar(); inOrder[inl++] = pop(stack, &length); } } if(n > 1) genPostOder(preOrder, inOrder, n); printf("%d\n", preOrder[0]); return 0; }