-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1099.cpp
68 lines (62 loc) · 1.2 KB
/
1099.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
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// 1099.cpp
// 算法
//
// Created by 王怡凡 on 2017/3/23.
// Copyright © 2017年 王怡凡. All rights reserved.
//
#include<cstdio>
#include<queue>
#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(root==-1) {
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);
int i;
for(i=0;i<n;i++){
scanf("%d%d",&lchild,&rchild);
Node[i].lchild = lchild;
Node[i].rchild = rchild;
}
for(i=0;i<n;i++) {
scanf("%d",&in[i]);
}
sort(in,in+n);
inOrder(0);
BFS(0);
return 0;
}