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
题目大意:通过中序遍历和后序遍历构建一棵树, 并交替的输出层序遍历;
思路:treeTraverse()构建这棵树, levelOrder()交替的层序遍历这棵树;
在构建这棵树的时候, 记录下每个节点的深度,根据节点深度的不同, 把节点添加到不同的数组中, 就可以实现交替的层序遍历
深度为奇数的层从左到右遍历, 复制从右到左遍历
1 #include<iostream> 2 #include<vector> 3 #include<queue> 4 using namespace std; 5 vector<int> in(30), post(30), depth(30, 0), leftToRight[30], rightToleft[30]; 6 int tree[30][2], root, idx1=0, idx2=0; 7 void treeTraverse(int &index, int height, int inl, int inr, int postl, int postr){ 8 int temp=post[postr], i=inl; 9 if(inl>inr) return; 10 index = postr; 11 depth[index] = height; //记录每个节点的深度 12 while(i<=inr && in[i]!=temp) i++; 13 treeTraverse(tree[index][0], height+1, inl, i-1, postl, postl+(i-inl)-1); 14 treeTraverse(tree[index][1], height+1, i+1, inr, postl+i-inl, postr-1); 15 } 16 17 void levelOrder(){ 18 queue<int> q; 19 q.push(root); 20 int height=0, flag=true; 21 while(q.size()){ 22 int temp = q.front(); 23 q.pop(); 24 //节点深度不同时, 表示开始遍历新的一层, 把原来的序号+1, 25 //替换新的深度, 更改标识符 26 //idx1是leftToRight的下标, idx2是rightToleft的下标 27 if(depth[temp]!=height){ 28 if(flag) idx2++; 29 else idx1++; 30 flag = 1-flag; 31 height=depth[temp]; 32 } 33 //根据节点的深度不同, 把节点添加到不同的数组中 34 if(depth[temp]%2==0) rightToleft[idx2].push_back(post[temp]); 35 else leftToRight[idx1].push_back(post[temp]); 36 if(tree[temp][0]!=-1) q.push(tree[temp][0]); 37 if(tree[temp][1]!=-1) q.push(tree[temp][1]); 38 } 39 } 40 int main(){ 41 int n, i, j; 42 scanf("%d", &n); 43 fill(tree[0], tree[0]+60, -1); 44 for(i=0; i<n; i++) scanf("%d", &in[i]); 45 for(i=0; i<n; i++) scanf("%d", &post[i]); 46 treeTraverse(root, 0, 0, n-1, 0, n-1); 47 levelOrder(); 48 printf("%d", rightToleft[0][0]); 49 for(i=1; i<idx1+idx2+1; i++){ 50 for(j=rightToleft[i/2].size()-1; j>=0 && i%2==0; j--) printf(" %d", rightToleft[i/2][j]); 51 for(j=0; j<leftToRight[i/2].size() && i%2==1; j++) printf(" %d", leftToRight[i/2][j]); 52 } 53 return 0; 54 }