-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA_1102.cpp
More file actions
96 lines (90 loc) · 1.47 KB
/
Copy pathA_1102.cpp
File metadata and controls
96 lines (90 loc) · 1.47 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 12;
int pre[maxn], in[maxn], post[maxn], n, num = 0;
bool hashtable[maxn] = {false};
struct Node
{
int data;
int lchild;
int rchild;
}node[maxn];
int strToNum(char c)
{
if (c == '-')
return -1;
hashtable[c-'0'] = true;
return c-'0';
}
int findRoot()
{
for(int i = 0; i < n; i++)
{
if(hashtable[i] == false)
return i;
}
}
void print(int root)
{
printf("%d", root);
num++;
if(num < n)
printf(" ");
else
printf("\n");
}
void layerorder(int root)
{
if(root == -1)
return;
queue<int> q;
q.push(root);
while(!q.empty())
{
int now = q.front();
q.pop();
print(now);
if(node[now].lchild != -1)
q.push(node[now].lchild);
if(node[now].rchild != -1)
q.push(node[now].rchild);
}
}
void inorder(int root)
{
if(root == -1)
return;
inorder(node[root].lchild);
print(root);
inorder(node[root].rchild);
}
void postorder(int root)
{
if(root == -1)
return;
postorder(node[root].lchild);
postorder(node[root].rchild);
swap(node[root].lchild, node[root].rchild);
}
int main()
{
scanf("%d", &n);
char c1, c2;
int x;
for(int i = 0; i < n; i++)
{
getchar();
scanf("%c %c", &c1, &c2);
node[i].lchild = strToNum(c1);
node[i].rchild = strToNum(c2);
}
int root = findRoot();
postorder(root);
layerorder(root);
num = 0;
inorder(root);
return 0;
}