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
   | #include<iostream> #include<cstring> #include<algorithm>
  using namespace std;
  const int N=110;
  int n; int w[N], l[N], r[N]; int a[N], q[N];
  void dfs(int u,int& k) {     if(u==-1)   return;     dfs(l[u], k);     w[u]=a[k++];     dfs(r[u], k); }
  void bfs() {     int hh=0, tt=0;     q[0]=0;     while(hh<=tt)     {         int t=q[hh++];         printf("%d ", w[t]);         if(l[t]!=-1)             q[++tt]=l[t];         if(r[t]!=-1)             q[++tt]=r[t];     } }
  int main() {     scanf("%d", &n);     for(int i=0;i<n;i++)         scanf("%d%d", &l[i], &r[i]);     for(int i=0;i<n;i++)         scanf("%d", &a[i]);     sort(a, a+n);          int k=0;     dfs(0, k);     bfs();          return 0; }
 
  |