首页 > 其他 > 详细

PAT A1127 ZigZagging on a Tree (30 分)

时间:2019-03-08 13:35:51      阅读:197      评论:0      收藏:0      [点我收藏+]

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 分)

原文:https://www.cnblogs.com/bobyin/p/10495084.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!