树中出现了按层或者顺序编号的情景,首先想到用树的静态写法。

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=110;
int n;
struct node{
	int data;
	int left,right;
}Node[maxn];
vector<int> num,layer;
void inorder(int root){   
	if(root==-1) return;
	inorder(Node[root].left);
	num.push_back(root);
	inorder(Node[root].right);
}
void layerorder(int root){
	queue<int> q;
	q.push(root);
	while(!q.empty()){
		int temp = q.front();
		layer.push_back(Node[temp].data);
		q.pop();
		if(Node[temp].left != -1) q.push(Node[temp].left);
		if(Node[temp].right != -1) q.push(Node[temp].right);
	}
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d%d",&Node[i].left,&Node[i].right);
	}
	vector<int> org;
	int u;
	for(int i=1;i<=n;i++){
		scanf("%d",&u);
		org.push_back(u);
	}
	sort(org.begin(),org.end());
	inorder(0);
	for(int i=0;i<n;i++){
		Node[num[i]].data = org[i];
	}
	layerorder(0);
	for(int i=0;i<layer.size();i++){
		printf(i==0?"%d":" %d",layer[i]);
	}
	return 0;
}