1. 程式人生 > >PAT A1127 ZigZagging on a Tree (30 分)

PAT A1127 ZigZagging on a Tree (30 分)

圖片 clas != child tween ins put enc algo

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. However, if you think the problem is too simple, then you are too naive. This time you are supposed to print the numbers in "zigzagging order" -- that is, starting from the root, print the numbers level-by-level, alternating between left to right and right to left. For example, for the following tree you must output: 1 11 5 8 17 12 20 15.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the inorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the zigzagging sequence of the tree in a line. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

8
12 11 20 17 1 15 8 5
12 20 17 11 15 8 5 1

  

Sample Output:

1 11 5 8 17 12 20 15

  

result:

技術分享圖片
 1 #include<cstdio>
 2 #include<vector>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn = 33;
 6 int n,postOrder[maxn],inOrder[maxn];
 7 vector<vector<int> > ans(maxn); 
 8 int level1=0;
 9 
10 void createTree(int inL,int inR,int postL,int postR,int level2){
11     if(postR<postL || inR<inL) return ;
12     int data = postOrder[postR];
13     ans[level2].push_back(data);
14 //    printf("%d ",data);
15     level1 = max(level1,level2);
16     int num=0;
17     while(inOrder[inL+num] != postOrder[postR]){
18         num++;
19     }
20     // 這樣生成的num指的是有num個lchild;
21     createTree(inL,inL+num-1,postL,postL +num-1,level2+1);
22     createTree(inL+num+1,inR,postL +num,postR - 1,level2+1);
23     return ;
24 }
25 
26 
27 int main(){
28     scanf("%d",&n);
29     for(int i=0;i<n;i++){
30         scanf("%d",&inOrder[i]);
31     }
32     for(int i=0;i<n;i++){
33         scanf("%d",&postOrder[i]);
34     }
35     createTree(0,n-1,0,n-1,0);
36     int s = 0;
37     bool flag = false;
38     for(vector<vector<int> >::iterator i=ans.begin();i!=ans.end();++i){
39         if(flag){
40             for(vector<int>::iterator j=(*i).begin();j !=(*i).end();++j){
41                 printf("%d",(*j));
42                 s++;
43                 if(s<n) printf(" ");
44             }
45             flag=false;
46         }else{
47             reverse((*i).begin(),(*i).end());
48             for(vector<int>::iterator j=(*i).begin(); j !=(*i).end();++j){
49                 printf("%d",(*j));
50                 s++;
51                 if(s<n) printf(" ");
52             }
53             flag=true;
54         }
55     }
56 }
View Code

PAT A1127 ZigZagging on a Tree (30 分)