-
Notifications
You must be signed in to change notification settings - Fork 0
/
A1099.cpp
56 lines (51 loc) · 941 Bytes
/
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
55
#include <cstdio>
#include <queue>// 用BFS做层遍历
#include <algorithm>
using namespace std;
const int maxn = 110;
struct node{
int data;
int lchild, rchild;
}Node[maxn];
int n, in[maxn], num = 0;
void inOrder(int root){
if(-1 == root){
return;
}
inOrder(Node[root].lchild);
Node[root].data = in[num++];
inOrder(Node[root].rchild);
}
void BFS(int root){
queue<int> q;
q.push(root);
num = 0;
while(!q.empty()){
int now = q.front();
q.pop();
printf("%d", Node[now].data);
num++;
if(num < n)
printf(" ");
if(Node[now].lchild != -1)
q.push(Node[now].lchild);
if(Node[now].rchild != -1)
q.push(Node[now].rchild);
}
}
int main(){
int lchild, rchild;
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%d %d", &lchild, &rchild);
Node[i].lchild = lchild;
Node[i].rchild = rchild;
}
for(int i = 0; i < n; i++){
scanf("%d", &in[i]);
}
sort(in, in + n);
inOrder(0);
BFS(0);
return 0;
}