题目描述
C++代码
样例
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int a[200];
struct node {
int left = -1;
int right = -1;
int key;
}tree[200];
vector<int> t;
void Inord(int i)
{
if (i == -1)
{
return;
}
Inord(tree[i].left);
t.push_back(i);
Inord(tree[i].right);
}
void Print(vector<int>& result)
{
queue<int> q;
q.push(0);
while (!q.empty())
{
int t = q.front();
q.pop();
result.push_back(tree[t].key);
if (tree[t].left != -1)
{
q.push(tree[t].left);
}
if (tree[t].right != -1)
{
q.push(tree[t].right);
}
}
}
int main()
{
int N;
cin >> N;
int n = N;
int p = 0;
while (N--)
{
int a, b;
cin >> a >> b;
tree[p].left = a;
tree[p].right = b;
p++;
}
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(a, a + n);
Inord(0);
for (int i = 0; i < t.size(); i++)
{
tree[t[i]].key = a[i];
}
vector<int> result;
Print(result);
cout << result[0];
for (int i = 1; i < result.size(); i++)
{
cout << " " << result[i];
}
return 0;
}