-
Notifications
You must be signed in to change notification settings - Fork 0
/
1102_Invert_a_Binary_Tree.cpp
91 lines (81 loc) · 1.73 KB
/
1102_Invert_a_Binary_Tree.cpp
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
81
82
83
84
85
86
87
88
89
90
91
#include <bits/stdc++.h>
using namespace std;
typedef struct Node* node;
struct Node {
int val;
node left;
node right;
Node(int v) {
val = v;
left = NULL;
right = NULL;
}
};
map<int, pair<int, int> > m;
node buildTree(int r) {
node root = new Node(r);
if (m[r].first != -1) {
root->left = buildTree(m[r].first);
}
if (m[r].second != -1) {
root->right = buildTree(m[r].second);
}
return root;
}
void levelOrder(node root) {
queue<node> que;
que.push(root);
bool isFirst = true;
while (!que.empty()) {
node temp = que.front();
que.pop();
if (isFirst) {
cout << temp->val;
isFirst = false;
} else {
cout << " " << temp->val;
}
if (temp->left) que.push(temp->left);
if (temp->right) que.push(temp->right);
}
cout << endl;
}
bool isFirst = true;
void inOrder(node root) {
if (root == NULL) return;
inOrder(root->left);
if (isFirst) {
cout << root->val;
isFirst = false;
} else {
cout << " " << root->val;
}
inOrder(root->right);
}
int main() {
int n;
cin >> n;
vector<int> fa(n, -1);
for (int i = 0; i < n; ++i) {
char l, r;
cin >> l >> r;
pair<int, int> temp = {-1, -1};
if (isdigit(l)) {
fa[l - '0'] = i;
temp.second = l - '0';
}
if (isdigit(r)) {
fa[r - '0'] = i;
temp.first = r - '0';
}
m[i] = temp;
}
int r;
for (int i = 0; i < n; ++i)
if (fa[i] == -1) r = i;
node root = buildTree(r);
levelOrder(root);
inOrder(root);
cout << endl;
return 0;
}