#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 110;
struct Node
{
//int data;
int left, right;
}node[maxn];
int n,level[maxn],origin[maxn];
int num = 0;
void inorder(int root)
{
if (root == -1) return;
inorder(node[root].left);
level[root] = origin[num++];
inorder(node[root].right);
}
void bfs(int root)
{
queue<int> q;
q.push(root);
int count = 0;
while (!q.empty())
{
count++;
int a = q.front();
printf("%d", level[a]);
if (count < n) printf(" ");
q.pop();
if (node[a].left != -1)
{
q.push(node[a].left);
}
if (node[a].right != -1)
{
q.push(node[a].right);
}
}
return;
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d %d", &node[i].left, &node[i].right);
}
for (int i = 0; i < n; i++)
{
scanf("%d", &origin[i]);
}
sort(origin, origin + n);
inorder(0);
bfs(0);
return 0;
}
A1099 Build A Binary Search Tree (30 分)
最新推荐文章于 2022-02-13 14:40:52 发布