-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathA1099.cpp
54 lines (48 loc) · 1.06 KB
/
A1099.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
53
54
#include<cstdio>
#include<algorithm>
#include<queue>
#define MAXN 110
using namespace std;
struct Node {
int data, left, right;
} bst[MAXN]; // 静态的二叉查找树
int n, left, right, numbers[MAXN], len = 0;
void inorder(int root)
{
if (bst[root].left != -1)
inorder(bst[root].left);
bst[root].data = numbers[len++];
if (bst[root].right != -1)
inorder(bst[root].right);
}
void layerorder(int root)
{
queue<int> q;
q.push(0);
while (!q.empty())
{
int index = q.front();
q.pop();
printf(--n ? "%d " : "%d", bst[index].data);
if (bst[index].left != -1)
q.push(bst[index].left);
if (bst[index].right != -1)
q.push(bst[index].right);
}
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d %d", &left, &right);
bst[i].left = left;
bst[i].right = right;
}
for (int i = 0; i < n; i++)
scanf("%d", &numbers[i]);
sort(numbers, numbers + n);
inorder(0);
layerorder(0);
return 0;
}