PAT_A1127#ZigZagging on a Tree

Source:

PAT A1127 ZigZagging on a Tree (30 分)

Description:

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.

zigzag.jpg

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

Keys:

Attention:

  • 开始方向弄反了-,-

Code:

 1 /*
 2 Data: 2019-05-31 21:17:07
 3 Problem: PAT_A1127#ZigZagging on a Tree
 4 AC: 33:50
 5 
 6 题目大意:
 7 假设树的键值为不同的正整数;
 8 给出二叉树的中序和后序遍历,输出二叉树的“之字形”层次遍历;
 9 
10 基本思路:
11 建树,层次遍历,依次用队列和堆存储结点值,输出即可
12 */
13 
14 #include<cstdio>
15 #include<stack>
16 #include<queue>
17 using namespace std;
18 const int M=35;
19 int in[M],post[M];
20 struct node
21 {
22     int data,layer;
23     node *lchild,*rchild;
24 };
25 
26 node *Create(int postL, int postR, int inL, int inR)
27 {
28     if(postL > postR)
29         return NULL;
30     node *root = new node;
31     root->data = post[postR];
32     int k;
33     for(k=inL; k<=inR; k++)
34         if(in[k]==root->data)
35             break;
36     int numLeft = k-inL;
37     root->lchild = Create(postL, postL+numLeft-1, inL,k-1);
38     root->rchild = Create(postL+numLeft, postR-1, k+1,inR);
39     return root;
40 }
41 
42 void Travel(node *root)
43 {
44     queue<node*> q;
45     stack<node*> s;
46     root->layer=1;
47     q.push(root);
48     while(!q.empty())
49     {
50         root = q.front();q.pop();
51         if(root->layer%2==0)
52         {
53             while(!s.empty())
54             {
55                 printf(" %d", s.top()->data);
56                 s.pop();
57             }
58             printf(" %d", root->data);
59         }
60         else{
61             if(root->layer==1)
62                 printf("%d", root->data);
63             else
64                 s.push(root);
65         }
66         if(root->lchild){
67             root->lchild->layer=root->layer+1;
68             q.push(root->lchild);
69         }
70         if(root->rchild){
71             root->rchild->layer=root->layer+1;
72             q.push(root->rchild);
73         }
74     }
75     while(!s.empty())
76     {
77         printf(" %d", s.top()->data);
78         s.pop();
79     }
80 }
81 
82 int main()
83 {
84 #ifdef    ONLINE_JUDGE
85 #else
86     freopen("Test.txt", "r", stdin);
87 #endif
88 
89     int n;
90     scanf("%d", &n);
91     for(int i=0; i<n; i++)
92         scanf("%d", &in[i]);
93     for(int i=0; i<n; i++)
94         scanf("%d", &post[i]);
95     node *root = Create(0,n-1,0,n-1);
96     Travel(root);
97 
98     return 0;
99 }

 

posted @ 2019-05-31 21:59  林東雨  阅读(672)  评论(0编辑  收藏  举报