直接中序往里面填就好了。。。。
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#define MAX 110
#define null -1
using namespace std;
int n;
struct Node {
int data;
int left;
int right;
};
Node t[MAX];
vector <int> a;
int pos = 0;
vector <int> ans;
void InOrder(int root) {
if (root != null) {
InOrder(t[root].left);
t[root].data = a[pos++];
InOrder(t[root].right);
}
}
void LevelOrder(int root) {
if (root != null) {
queue <int> q;
q.push(root);
while (!q.empty()) {
int top = q.front();
q.pop();
ans.push_back(t[top].data);
if (t[top].left != null) {
q.push(t[top].left);
}
if (t[top].right != null) {
q.push(t[top].right);
}
}
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
t[i].data = i;
cin >> t[i].left >> t[i].right;
}
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
InOrder(0);
LevelOrder(0);
cout << ans[0];
for (int i = 1; i < n; i++) {
cout << " " << ans[i];
}
return 0;
}