1086 Tree Traversals Again (25分)(树的重构与遍历)

1086 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 (≤) 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


这个题,我一开始只看到push是按照前序遍历顺序push的,也看到了pop是按照中序遍历顺序pop的,但是没有把二者结合在一起。
蠢到死了啊啊啊。
所以上面说的,就是已经知道前序遍历和中序遍历求后序遍历。
 1 /*
 2     Push的顺序为前序遍历,Pop的顺序为中序遍历,所以这道题就是
 3     已知前序遍历和中序遍历求后序遍历。
 4 */
 5 #include <iostream>
 6 #include <string>
 7 #include <vector>
 8 #include <stack>
 9 #include <algorithm>
10 using namespace std;
11 
12 vector <int> pre, in, post;
13 
14 typedef struct Tree {
15     int data;
16     Tree *l, *r;
17 } *ptree;
18 
19 ptree build(int pl, int pr, int il, int ir) {
20     if(pl > pr || il > ir) return NULL;
21     int pos;
22     for(int i = il; i <= ir; i ++) {
23         if(in[i] == pre[pl]) {
24             pos = i;
25             break;
26         }
27     }
28     ptree root = new Tree;
29     root -> data = in[pos];
30     root -> l = root -> r = NULL;
31     root -> l = build(pl + 1, pl + pos - il, il, pos - 1);
32     root -> r = build(pl + pos - il + 1, pr, pos + 1, ir);
33     return root;
34 }
35 
36 void post_order(ptree root) {
37     if(!root) return;
38     post_order(root -> l);
39     post_order(root -> r);
40     post.push_back(root -> data);
41 }
42 
43 int main() {
44     string op;
45     int n, num;
46     cin >> n;
47     stack <int> s;
48     pre.push_back(-1);
49     in.push_back(-1);
50     for(int i = 0; i < 2 * n; i ++) {
51         cin >> op;
52         if(op == "Push") {
53             cin >> num;
54             pre.push_back(num);
55             s.push(num);
56         } else {
57             in.push_back(s.top());
58             s.pop();
59         }
60     }
61     post_order(build(1, n, 1, n));
62     for(int i = 0; i < post.size(); i ++) {
63         if(i) cout << " ";
64         cout << post[i];
65     }
66     cout << endl;
67     return 0;
68 }
 

猜你喜欢

转载自www.cnblogs.com/bianjunting/p/13166569.html