码迷,mamicode.com
首页 > 其他好文 > 详细

pat1086. Tree Traversals Again (25)

时间:2015-09-06 09:50:57      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

1086. Tree Traversals Again (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 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<stack>
 3 #include<algorithm>
 4 #include<iostream>
 5 #include<stack>
 6 #include<set>
 7 #include<map>
 8 #include<vector>
 9 using namespace std;
10 int per[35],in[35];
11 stack<int> s;
12 int n;
13 void Build(int *per,int *in,int num){
14     if(num==0){
15         return;
16     }
17     int mid=per[0];
18 
19     //cout<<mid<<endl;
20 
21     int i;
22     for(i=0;i<num;i++){
23         if(in[i]==mid){
24             break;
25         }
26     }
27 
28     //cout<<num<<endl;
29 
30     Build(per+1,in,i);
31     Build(per+1+i,in+1+i,num-1-i);
32     if(num==n){
33         printf("%d",mid);
34     }
35     else{
36         printf("%d ",mid);
37     }
38 }
39 int main(){
40     //freopen("D:\\INPUT.txt","r",stdin);
41     scanf("%d",&n);
42     n*=2;
43     int i,num,pi=0,ini=0;
44     string op;
45     for(i=0;i<n;i++){
46         cin>>op;
47         if(op=="Push"){
48             scanf("%d",&num);
49             s.push(num);
50             per[pi++]=num;
51         }
52         else{
53             in[ini++]=s.top();
54             s.pop();
55         }
56     }
57     n=n/2;
58 
59     /*cout<<n<<endl;
60     for(i=0;i<n;i++){
61         cout<<per[i]<<" ";
62     }
63     cout<<endl;
64     for(i=0;i<n;i++){
65         cout<<in[i]<<" ";
66     }
67     cout<<endl;*/
68 
69     Build(per,in,n);
70     printf("\n");
71     return 0;
72 }

 

pat1086. Tree Traversals Again (25)

标签:

原文地址:http://www.cnblogs.com/Deribs4/p/4784479.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!