版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zero_1778393206/article/details/86548028
#include<iostream>
#include<cstdlib>
#include<queue>
#include<algorithm>
using namespace std;
typedef struct BinaryNode
{
int data;
int left;
int right;
}BTree;
void Assignment(BTree B[100], int num[100],int r);
int c;
void Print(BTree B[100],int num[100]);
int main()
{
int n,t;
BTree B[100];
int num[100];
cin >> n;
t = 0;
while (t<n)
{
int a, b;
cin >> a >> b;
B[t].left = a;
B[t].right = b;
t++;
}
t = 0;
while (t<n)
{
cin >> num[t++];
}
sort(num,num+n);
c = 0;
Assignment(B, num,0);
c = 0;
Print(B,num);
t = 0;
while (t < n)
{
if (t == 0)
cout << num[t++];
else
cout << " " << num[t++];
}
cout << endl;
system("pause");
return 0;
}
void Assignment(BTree B[100], int num[100],int r)
{
if (r == -1)
return;
Assignment(B, num, B[r].left);
B[r].data = num[c++];
Assignment(B, num, B[r].right);
}
void Print(BTree B[100],int num[100])
{
queue<int> Q;
Q.push(0);
while (!Q.empty())
{
int p = Q.front();
Q.pop();
num[c++] = B[p].data;
if (B[p].left != -1)
Q.push(B[p].left);
if (B[p].right != -1)
Q.push(B[p].right);
}
}