-
Notifications
You must be signed in to change notification settings - Fork 814
/
Copy path1102. Invert a Binary Tree (25).cpp
52 lines (52 loc) · 1.27 KB
/
1102. Invert a Binary Tree (25).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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct node {
int id, l, r, index, level;
} a[100];
vector<node> v1;
void dfs(int root, int index, int level) {
if (a[root].r != -1) dfs(a[root].r, index * 2 + 2, level + 1);
v1.push_back({root, 0, 0, index, level});
if (a[root].l != -1) dfs(a[root].l, index * 2 + 1, level + 1);
}
bool cmp(node a, node b) {
if (a.level != b.level) return a.level < b.level;
return a.index > b.index;
}
int main() {
int n, have[100] = {0}, root = 0;
cin >> n;
for (int i = 0; i < n; i++) {
a[i].id = i;
string l, r;
cin >> l >> r;
if (l != "-") {
a[i].l = stoi(l);
have[stoi(l)] = 1;
} else {
a[i].l = -1;
}
if (r != "-") {
a[i].r = stoi(r);
have[stoi(r)] = 1;
} else {
a[i].r = -1;
}
}
while (have[root] == 1) root++;
dfs(root, 0, 0);
vector<node> v2(v1);
sort(v2.begin(), v2.end(), cmp);
for (int i = 0; i < v2.size(); i++) {
if (i != 0) cout << " ";
cout << v2[i].id;
}
cout << endl;
for (int i = 0; i < v1.size(); i++) {
if (i != 0) cout << " ";
cout << v1[i].id;
}
return 0;
}