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 }
时间: 2024-08-14 01:06:22

pat03-树3. Tree Traversals Again (25)的相关文章

1086. Tree Traversals Again (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1086. Tree Traversals Again (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B 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

pat1086. Tree Traversals Again (25)

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 th

03-树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 th

03-3. Tree Traversals Again (25)

03-3. 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:

PAT Advanced 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); po

A1086. 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()

PAT 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()

1020 Tree Traversals (25 分)

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree. Input Specification: Each

1086. Tree Traversals Again (25)

时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue push 的顺序就是二叉树的前序 pop的顺序就是二叉树的中序遍历 本质上还是考根据这两个顺序建立二叉树,并且进行后序遍历 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when