静态数组,不多说了
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 110;
int N;
vector<int> v;
struct node{
int data;
int left;
int right;
}Node[MAXN];
queue<struct node> q;
int ind = 0;
void inorder(int root){
if (root == -1) {
return;
}
inorder(Node[root].left);
Node[root].data = v[ind++];
inorder(Node[root].right);
}
int space = 0;
void levelorder(struct node root){
q.push(root);
while (!q.empty()) {
struct node temp = q.front();
q.pop();
printf("%d",temp.data);
space++;
if (space < N) {
printf(" ");
}
if (temp.left != -1) {
q.push(Node[temp.left]);
}
if (temp.right != -1) {
q.push(Node[temp.right]);
}
}
}
int main(){
// printf("123\n");
scanf("%d",&N);
for (int i=0; i<N; i++) {
int a,b;
scanf("%d%d",&a,&b);
Node[i].left = a;
Node[i].right = b;
}
for (int i=0; i<N; i++) {
int val;
scanf("%d",&val);
v.push_back(val);
}
sort(v.begin(), v.end());
inorder(0);
levelorder(Node[0]);
}