pat03-树3. Tree Traversals Again (25)

03-树3. Tree Traversals Again (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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

提交代码

已知前序和中序,求后序。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<queue>
 6 #include<stack>
 7 using namespace std;
 8 queue<int> q;
 9 void Buildin(int *pre,int *in,int inlen){
10     if(!inlen){
11         return;
12     }
13     int c=pre[0];
14     int i;
15     for(i=0;i<inlen;i++){
16         if(c==in[i]){
17             break;
18         }
19     }
20     Buildin(pre+1,in,i);
21     Buildin(pre+i+1,in+i+1,inlen-i-1);
22     q.push(pre[0]);
23 }
24 int pre[35],in[35];
25 int main(){
26     //freopen("D:\INPUT.txt","r",stdin);
27     //freopen("D:\OUTPUT.txt","w",stdout);
28     int n;
29     string op;
30     stack<int> s;
31     //int *pre=new int[n+1];
32     //int *in=new int[n+1];
33     scanf("%d",&n);
34 
35     //cout<<n<<endl;
36 
37 
38     int i,prep=0,inp=0;
39     n*=2;
40     for(i=0;i<n;i++){
41         cin>>op;
42         if(op=="Push"){
43             scanf("%d",&pre[prep]);
44             s.push(pre[prep++]);
45         }
46         else{
47             in[inp++]=s.top();
48             s.pop();
49         }
50     }
51 
52     /*for(i=0;i<n/2;i++){
53         cout<<pre[i]<<endl;
54     }
55     cout<<endl;
56     for(i=0;i<n/2;i++){
57         cout<<in[i]<<endl;
58     }
59     cout<<endl;
60     cout<<i<<endl;*/
61 
62     Buildin(pre,in,n/2);
63 
64 
65 /*    cout<<endl;
66     cout<<i<<endl;*/
67 
68     printf("%d",q.front());
69     q.pop();
70     while(!q.empty()){
71         printf(" %d",q.front());
72         q.pop();
73     }
74     return 0;
75 }
原文地址:https://www.cnblogs.com/Deribs4/p/4732270.html