PAT 1086 Tree Traversals Again

PAT 1086 Tree Traversals Again

题目:

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

地址:http://pat.zju.edu.cn/contests/pat-a-practise/1086

    从非递归中序遍历中出栈与入栈的序列中找出这个棵树的后序遍历序列。很多人的做法是重新构造出这棵树,但我的方法不需要构造出一棵树,只需要利用两个栈,并且向后多看一个序列即可完成。首先,第一栈是用来模拟原来中序序列的出栈和入栈,第二栈是用来存储有右孩子的根节点的。整个算法就是在分析这个出栈入栈的序列,如果当前序列为入栈,则我们把对应的节点压入第一个栈中,如果当前为出栈序列,则可分为两种情况。第一,后面紧跟着是入栈操作,说明此时出栈的节点要往右孩子那边遍历,所有把该出栈的节点压入第二个栈,并且记下该节点的右孩子值;第二,后面紧跟着还是出栈操作或者为最后一个出栈操作,此时第一个栈出栈一个节点,如果第二个栈非空,并且第二个栈的栈顶节点的右孩子为第一个栈的出栈节点,那么输出右孩子节点,第二个栈出栈,注意这里是一个while循环,因为后序遍历有可能是顺着右节点往上输出的。代码:

 1 #include <string>
 2 #include <vector>
 3 #include <iostream>
 4 #include <stack>
 5 using namespace std;
 6 
 7 void print(int t, int &flag)
 8 {
 9     if(flag){
10         flag = 0;
11         cout << t;
12     }else{
13         cout << ' ' << t;
14     }
15 }
16 
17 int main()
18 {
19     int n;
20     const string s1 = "Push";
21     const string s2 = "Pop";
22     while(cin >> n){
23         n <<= 1;
24         vector<string> input(n,"");
25         vector<int> val(n,0);
26         for(int i = 0; i < n; ++i){
27             cin >> input[i];
28             if(input[i] == s1)
29               cin >> val[i];
30         }
31         stack<int> stk;
32         stack<int> root;
33         vector<int> rightchild(n>>1,0);
34         int flag = 1;
35         for(int i = 0; i < n; ++i){
36             if(input[i] == s1){
37                 stk.push(val[i]);
38             }else{
39                 if(i+1 >= n || input[i+1] == s2){
40                     int t = stk.top();
41                     stk.pop();
42                     if(root.empty()){
43                         print(t,flag);
44                     }else{
45                         while(!root.empty() && rightchild[root.top()] == t){
46                             print(t,flag);
47                             t = root.top();
48                             root.pop();
49                         }
50                         print(t,flag);
51                     }
52                 }else{
53                     int t = stk.top();
54                     stk.pop();
55                     root.push(t);
56                     rightchild[t] = val[i+1];
57                 }
58             }
59 
60         }
61         cout << endl;
62     }
63     return 0;
64 }
原文地址:https://www.cnblogs.com/boostable/p/pat_1086.html