zl程序教程

您现在的位置是:首页 >  其它

当前栏目

1102 Invert a Binary Tree

Tree Binary
2023-09-11 14:22:44 时间

The following is from Max Howell @twitter:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off.
 

Now it's your turn to prove that YOU CAN invert a binary tree!

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node from 0 to N−1, and gives the indices of the left and right children of the node. If the child does not exist, a - will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each test case, print in the first line the level-order, and then in the second line the in-order traversal sequences of the inverted tree. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
 

Sample Output:

3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1

 

题意:

  给出一棵树,将其反转,然后输出层次遍历和中序遍历的结果。

思路:

  Invert 的意思就是将每一棵树的左子树和右子树互换,那么我们在输入的时候,就将这一步完成,接下来按照普通的树来建树就好了。

Code:

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

 


2020-07-12 22:33:35

一种不用建树的方法:https://blog.csdn.net/liuchuo/article/details/52175736

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5 struct node {
 6     int id, l, r, index, level;
 7 } a[100];
 8 vector<node> v1;
 9 void dfs(int root, int index, int level) {
10     if (a[root].r != -1) dfs(a[root].r, index * 2 + 2, level + 1);
11     v1.push_back({root, 0, 0, index, level});
12     if (a[root].l != -1) dfs(a[root].l, index * 2 + 1, level + 1);
13 }
14 bool cmp(node a, node b) {
15     if (a.level != b.level) return a.level < b.level;
16     return a.index > b.index;
17 }
18 int main() {
19     int n, have[100] = {0}, root = 0;
20     cin >> n;
21     for (int i = 0; i < n; i++) {
22         a[i].id = i;
23         string l, r;
24         cin >> l >> r;
25         if (l != "-") {
26             a[i].l = stoi(l);
27             have[stoi(l)] = 1;
28         } else {
29             a[i].l = -1;
30         }
31         if (r != "-") {
32             a[i].r = stoi(r);
33             have[stoi(r)] = 1;
34         } else {
35             a[i].r = -1;
36         }
37     }
38     while (have[root] == 1) root++;
39     dfs(root, 0, 0);
40     vector<node> v2(v1);
41     sort(v2.begin(), v2.end(), cmp);
42     for (int i = 0; i < v2.size(); i++) {
43         if (i != 0) cout << " ";
44         cout << v2[i].id;
45     }
46     cout << endl;
47     for (int i = 0; i < v1.size(); i++) {
48         if (i != 0) cout << " ";
49         cout << v1[i].id;
50     }
51     return 0;
52 }