#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int n;
vector <int> ans;
struct node
{
int lchild,rchild;
int num;
}nodes[105];
int f(int x)
{
if(x==-1)
return 0;
return f(nodes[x].lchild)+f(nodes[x].rchild)+1;
}
void build(int r,int L,int R)
{
if(r==-1)
return;
int n=f(nodes[r].lchild);
nodes[r].num=ans[L+n];
build(nodes[r].lchild,L,L+n-1);
build(nodes[r].rchild,L+n+1,R);
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
cin>>nodes[i].lchild>>nodes[i].rchild;
ans.resize(n);
for(int i=0;i<n;i++)
cin>>ans[i];
sort(ans.begin(),ans.end());
build(0,0,n-1);
//for(int i=0;i<n;i++)
//cout<<nodes[i].num<<endl;
bool flag=false;
queue <int> que;
que.push(0);
while(!que.empty())
{
int tmp=que.front();
if(flag)
{
cout<<' '<<nodes[tmp].num;
}
else
{
cout<<nodes[tmp].num;
flag=1;
}
que.pop();
if(nodes[tmp].lchild!=-1)
{
que.push(nodes[tmp].lchild);
}
if(nodes[tmp].rchild!=-1)
{
que.push(nodes[tmp].rchild);
}
}
return 0;
}
1099. Build A Binary Search Tree (30)
最新推荐文章于 2023-03-16 20:58:51 发布