码迷,mamicode.com
首页 > 其他好文 > 详细

1127 ZigZagging on a Tree

时间:2020-04-22 12:51:03      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:技术   zigzag   one   line   distinct   tree   native   imp   first   

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

 

题意:

  给出中序和后序遍历的结果,要求按照从右到左,从左到右依次替换的方法来输出层次遍历的结果。(Too native.)

思路:

  构造数,层次遍历,遍历的过程中用双向队列来存储每一层的数据,没遍历完一层,根据要求判断所在层数是pop_front()还是pop_back()。

Code:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 vector<int> inOrder(50);
 6 vector<int> postOrder(50);
 7 
 8 typedef struct Node* node;
 9 
10 struct Node {
11     int date;
12     node left;
13     node right;
14     Node(int v) {
15         date = v;
16         left = NULL;
17         right = NULL;
18     }
19 };
20 
21 node buildTree(int index, int left, int right) {
22     if (left > right) return NULL;
23     node root = new Node(postOrder[index]);
24     int pos;
25     for (int i = left; i <= right; ++i) {
26         if (inOrder[i] == postOrder[index]) {
27             pos = i;
28             break;
29         }
30     }
31     int len = right - pos;
32     root->left = buildTree(index - len - 1, left, pos - 1);
33     root->right = buildTree(index - 1, pos + 1, right);
34     return root;
35 }
36 
37 void levelOrder(node root) {
38     queue<node> que;
39     deque<int> level;
40     vector<int> ans;
41     node last = new Node(-1);
42     que.push(root);
43     que.push(last);
44     bool leftToRight = false;
45     while (!que.empty()) {
46         node temp = que.front();
47         que.pop();
48         if (temp->date == -1) {
49             if (que.empty() && level.empty()) break;
50             que.push(last);
51             if (leftToRight) {
52                 while (!level.empty()) {
53                     ans.push_back(level.front());
54                     level.pop_front();
55                 }
56                 leftToRight = false;
57             } else {
58                 while (!level.empty()) {
59                     ans.push_back(level.back());
60                     level.pop_back();
61                 }
62                 leftToRight = true;
63             }
64         } else {
65             level.push_back(temp->date);
66             if (temp->left) que.push(temp->left);
67             if (temp->right) que.push(temp->right);
68         }
69     }
70     cout << ans[0];
71     for (int i = 1; i < ans.size(); ++i) cout << " " << ans[i];
72     cout << endl;
73 }
74 
75 int main() {
76     int n;
77     cin >> n;
78 
79     for (int i = 0; i < n; ++i) cin >> inOrder[i];
80     for (int i = 0; i < n; ++i) cin >> postOrder[i];
81 
82     node root = buildTree(n - 1, 0, n - 1);
83 
84     levelOrder(root);
85 
86     return 0;
87 }

 

1127 ZigZagging on a Tree

标签:技术   zigzag   one   line   distinct   tree   native   imp   first   

原文地址:https://www.cnblogs.com/ruruozhenhao/p/12751174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!