-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1156_Binary_tree.cpp
More file actions
77 lines (64 loc) · 1.45 KB
/
Copy path1156_Binary_tree.cpp
File metadata and controls
77 lines (64 loc) · 1.45 KB
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
// Problem#: 1156
// Submission#: 1296404
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <queue>
#include <cstring>
#include <stack>
using namespace std;
char zi[1001];
int r[1001];
int l[1001];
char node[1001];
int fa[1001];
int n;
int head = 0;
int vis[1001];
queue<int > ca;
void go(){
memset(fa,0,sizeof(fa));
//memset(vis,0,sizeof(vis));
head = 0;
for(int i = 0;i < n;i ++){
int a,b,c;char k;
cin >> a >> k >> b >> c;
ca.push(a);
fa[b] = a;
fa[c] = a;
l[a] = b;
r[a] = c;
node[a] = k;
}
stack<int > ta;
while(!ca.empty()){
int temp = ca.front();
ca.pop();
if(fa[temp] == 0){
head = temp;
}
}
ta.push(head);
while(!ta.empty()){
int temp = ta.top();
//vis[temp] == 1;
ta.pop();
int b = l[temp];
int a = r[temp];
cout << node[temp];
if(a > 0){
ta.push(a);
// vis[a] = 1;
}
if(b > 0){
ta.push(b);
}
}
cout << endl;
}
int main(){
while(cin >> n){
go();
}
}