蛇形打印二叉树

1127 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

题目大意:给定二叉树的中序和后序遍历的数列,蛇形逐层打印二叉树。

思路:中序+后序,建立二叉树;(建树的具体实现方法和思路见我之前的文章:后序+中序,先序+中序,建立二叉树)建树的过程顺便记录每个节点所处的深度以及树的总深度;接着层序遍历,将每个节点的所属层数对应二维数组ans的行,依次push back~~最后将ans蛇形输出,奇数层(我是从0开始计数的)从左往右,偶数层反之。

 1 #include<iostream>
 2 #include<vector>
 3 #include<unordered_map>
 4 #include<queue>
 5 using namespace std;
 6 typedef struct node* BT;
 7 struct node {
 8     int value, level = 0;
 9     BT left = NULL, right = NULL;
10 };
11 int treeDepth = 0;//树的深度 
12 unordered_map<int, int> mp;//映射元素在中序数组里的位置 
13 vector<int> Inorder, Post, ans[30];
14 BT buildTree(int InLeft, int InRight, int PostIndex, int depth);
15 void levelOrder(BT t);
16 void printLtoR(vector<int> &v);
17 void printRtoL(vector<int> &v);
18 int main()
19 {
20     int N, i;
21     scanf("%d", &N);
22     Inorder.resize(N);
23     Post.resize(N);
24     for (i = 0; i < N; i++) {
25         scanf("%d", &Inorder[i]);
26         mp[Inorder[i]] = i;
27     }
28     for (i = 0; i < N; i++)
29         scanf("%d", &Post[i]);
30     BT tree = NULL;
31     tree = buildTree(0, N - 1, N - 1, 0);
32     levelOrder(tree);
33     for (i = 0; i <= treeDepth; i++) {
34         if (i % 2 == 0) printRtoL(ans[i]);
35         else printLtoR(ans[i]);
36         if (i <= treeDepth - 1)
37             printf(" ");
38     }
39     return 0;
40 }
41 void printRtoL(vector<int> &v)
42 {
43     int n = v.size();
44     for (int i = n - 1; i >= 0; i--) {
45         printf("%d", v[i]);
46         if (i > 0)
47             printf(" ");
48     }
49 }
50 void printLtoR(vector<int> &v)
51 {
52     int n = v.size();
53     for (int i = 0; i < n; i++) {
54         printf("%d", v[i]);
55         if (i < n - 1)
56             printf(" ");
57     }
58 }
59 void levelOrder(BT t)
60 {
61     queue<BT> Q;
62     Q.push(t);
63     while (!Q.empty()) {
64         BT tmp = Q.front();
65         ans[tmp->level].push_back(tmp->value);
66         Q.pop();
67         if (tmp->left)
68             Q.push(tmp->left);
69         if (tmp->right)
70             Q.push(tmp->right);
71     }
72 }
73 BT buildTree(int InLeft, int InRight, int PostIndex, int depth)
74 {
75     if (InLeft > InRight) return NULL;
76     if (treeDepth < depth) treeDepth = depth;
77     BT t = new node();
78     t->level = depth;
79     t->value = Post[PostIndex];
80     t->right = buildTree(mp[Post[PostIndex]] + 1, InRight, PostIndex - 1, depth + 1);
81     int rtreeNum = InRight - mp[Post[PostIndex]];//当前节点的右子树节点个数 
82     t->left = buildTree(InLeft, mp[Post[PostIndex]] - 1, PostIndex - rtreeNum - 1, depth + 1);
83     return t;
84 }

猜你喜欢

转载自www.cnblogs.com/yinhao-ing/p/10671019.html