-
Notifications
You must be signed in to change notification settings - Fork 0
/
1130.cpp
52 lines (40 loc) · 984 Bytes
/
1130.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 <string>
using namespace std;
const int MAXN = 20 + 10;
struct Node {
string data;
Node *lc, *rc;
Node *fa;
Node() {
data = "";
lc = rc = fa = nullptr;
}
void inOrder() {
if ((lc || rc) && fa) cout << '(';
if (lc) lc->inOrder();
cout << data;
if (rc) rc->inOrder();
if ((rc || rc) && fa) cout << ')';
}
} nodes[MAXN];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
string data;
int lc, rc;
cin >> data >> lc >> rc;
nodes[i].data = data;
if (lc != -1) nodes[i].lc = nodes + lc;
if (rc != -1) nodes[i].rc = nodes + rc;
nodes[lc].fa = nodes[rc].fa = nodes + i;
}
for (int i = 1; i <= n; i++) {
if (!nodes[i].fa) {
nodes[i].inOrder();
break;
}
}
return 0;
}