1099 Build A Binary Search Tree (30 分)
思路有空更新
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
struct node{
int l, r, value;
}a[200];
int b[200], ret;
void dfs(int x) {
if(a[x].l != -1) dfs(a[x].l);
a[x].value = b[ret++];
if(a[x].r != -1) dfs(a[x].r);
}
int main() {
int n, f = 0;
cin >> n;
for(int i = 0; i < n; ++i)
cin >> a[i].l >> a[i].r;
for(int i = 0; i < n; ++i)
cin >> b[i];
sort(b, b + n);
dfs(0);
queue<int> q;
q.push(0);
while(!q.empty()) {
int x = q.front();
q.pop();
if(f) cout << " ";
cout << a[x].value;
f = 1;
if(a[x].l != -1) q.push(a[x].l);
if(a[x].r != -1) q.push(a[x].r);
}
}