03-树3 Tree Traversals Again (25 分)

时间:2022-07-07 18:46:29

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.

03-树3 Tree Traversals Again (25 分)
Figure 1

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2 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



 1 #include <cstdio>
 2 #include <stack>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <iostream> 
 6 using namespace std;
 7 const int maxn = 32;
 8 const int smaxn = 10;
 9 
10 
11 
12 int pre[maxn], in[maxn], post[maxn];
13 void input(int ) ;
14 int str2num(char s[], int start);
15 void solve(int postL, int preL, int inL, int n ) ;
16 
17 
18 int main() {
19     int n;
20     scanf("%d", &n);
21     input(n);
22     solve(0, 0, 0, n);
23     int flag = 1;
24     for(int i=0; i<n; i++) {
25         if(flag == 1) {
26             printf("%d", post[i]);
27             flag = 0;
28         }
29         else printf(" %d", post[i]);
30     }
31     system("pause");
32     return 0;
33 } 
34 
35 void input(int n) {
36     stack<int> st;
37     char str[smaxn];
38     int preIndex = 0, inIndex = 0;
39     getchar();
40     for(int i=0; i<2*n; i++) {
41         cin.getline(str, maxn);
42         if(strlen(str) != 3) {
43             int tmp = str2num(str, 5);
44             pre[preIndex++] = tmp ;
45             st.push(tmp);
46         }
47         else {
48             in[inIndex++] = st.top();
49             st.pop();
50         }
51     }
52 
53 }
54 
55 int str2num(char s[], int start) 
56 {
57     int sum = 0;
58     for(; s[start]; start++) {
59         sum = sum*10 + s[start] - '0';
60     }
61     return sum;
62 }
63 
64 void solve(int postL, int preL, int inL, int n ) {
65 
66     if(n == 0) {
67         return;
68     }
69     int root = pre[preL];
70     int l_num, r_num, i;
71     post[postL+n-1] = root;
72     
73     for(i=0; i<n; i++) {
74         if(in[inL+i] == root) break;
75     }
76     l_num = i; r_num = n - i - 1;
77     
78     solve(postL, preL+1, inL, l_num);
79     solve(postL+l_num, preL+l_num+1, inL+i+1, r_num);
80     
81 
82 }