-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.cpp
More file actions
76 lines (67 loc) · 1.3 KB
/
tree.cpp
File metadata and controls
76 lines (67 loc) · 1.3 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
#include<bits/stdc++.h>
using namespace std;
struct node {
char val;
node *left, *right;
node(char s) : val(s), left(NULL), right(NULL) {}
};
node *find(node *root, char s) {
queue<node*> q; q.push(root);
while(!q.empty()) {
node *cur=q.front(); q.pop();
if(cur->val==s) return cur;
if(cur->left!=NULL) q.push(cur->left);
if(cur->right!=NULL) q.push(cur->right);
}
return NULL;
}
void pre(node *root) {
if(root==NULL) return;
cout << root->val;
pre(root->left);
pre(root->right);
}
void in(node *root) {
if(root==NULL) return;
in(root->left);
cout << root->val;
in(root->right);
}
void post(node *root) {
if(root==NULL) return;
post(root->left);
post(root->right);
cout << root->val;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
node *root = new node('A');
vector<string> v;
for (int i=0; i<n; i++) {
string a,b,c; cin>>a>>b>>c;
v.push_back(a+b+c);
}
for (int i=0; i<n; i++) {
int sz=v.size();
for (int j=0; j<sz; j++) {
string t=v[j];
char a=t[0], b=t[1], c=t[2];
node *cur=find(root,a);
if(cur!=NULL) {
if(b!='.') cur->left=new node(b);
if(c!='.') cur->right=new node(c);
v.erase(v.begin()+j); break;
}
}
}
pre(root);
cout << endl;
in(root);
cout << endl;
post(root);
cout << endl;
return 0;
}