PAT A1127 . ZigZagging on a Tree (30)

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <queue>
#include <set>
#include <map>
using namespace std;

const int maxn = 35;
int n, post[maxn], in[maxn];

vector<int> layerVec;
struct node{
int data, layer;
node *left, *right;
node(){left = right = NULL;}//初始化
};
node* create(int postL, int postR, int inL, int inR){
if(postL > postR) return NULL;
node *root = new node;
root->data = post[postR];
int k;
for(k = inL; k <= inR; k++){
if(in[k] == post[postR]) break;
}
int numLeft = k - inL;
root->left = create(postL, postL + numLeft - 1, inL, k - 1);
root->right = create(postL + numLeft, postR - 1, k + 1, inR);
return root;
}
int maxLev = 0, level[maxn] = {0};
void layerOrder(node *root){
queue<node*> q;
q.push(root);
root->layer = 1;//第一层
while(!q.empty()){
node *now = q.front();
q.pop();
layerVec.push_back(now->data);
level[now->layer]++;
maxLev = max(maxLev, now->layer);
if(now->left != NULL){
q.push(now->left);
now->left->layer = now->layer + 1;
}
if(now->right != NULL){
q.push(now->right);
now->right->layer = now->layer + 1;
}
}
}
int main(){

//freopen("in.txt", "r", stdin);
scanf("%d", &n);
for(int i = 0;i < n; i++){
scanf("%d", &in[i]);
}
for(int i = 0;i < n; i++){
scanf("%d", &post[i]);
}
node* root = create(0, n - 1, 0, n - 1);
layerOrder(root);
int cnt = 0;//统计当前打印的数目
for(int i = 1;i <= maxLev; i++){
if(i % 2 == 1){//奇数行翻转
reverse(layerVec.begin() + cnt, layerVec.begin() + cnt + level[i]);
}
cnt+= level[i];
}
for(int i = 0;i < n; i++){
printf("%d", layerVec[i]);
if(i < n - 1) printf(" ");
else printf("\n");
}
return 0;

}