1099 Build A Binary Search Tree (30分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than or equal to the node’s key.
Both the left and right subtrees must also be binary search trees.
Given the structure of a binary tree and a sequence of distinct integer keys, there is only one way to fill these keys into the tree so that the resulting tree satisfies the definition of a BST. You are supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤100) which is the total number of nodes in the tree. The next N lines each contains the left and the right children of a node in the format left_index right_index, provided that the nodes are numbered from 0 to N−1, and 0 is always the root. If one child is missing, then −1 will represent the NULL child pointer. Finally N distinct integer keys are given in the last line.
Output Specification:
For each test case, print in one line the level order traversal sequence of that tree. All the numbers must be separated by a space, with no extra space at the end of the line.
Sample Input:
9
1 6
2 3
-1 -1
-1 4
5 -1
-1 -1
7 -1
-1 8
-1 -1
73 45 11 58 82 25 67 38 42
Sample Output:
58 25 82 11 38 67 45 73 42
用邻接表模拟建树,中序遍历记录大小关系,层序出结果。
#include <bits/stdc++.h>
#include <limits.h>
#include <math.h>
#include <memory.h>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ms(x, n) memset(x, n, sizeof(x))
#define wht() \
int _; \
for (scanf("%d", &_); _; _--)
#define For(i, a, b) for (int i = a; i <= (b); ++i)
#define Rof(i, a, b) for (int i = a; i >= (b); --i)
#define ioss ios::sync_with_stdio(false)
#define all(a) a.begin(), a.end()
#define ll long long
// #define input(x) scanf("%lld", &x);
// #define print(x) printf("%lld", x);
// #define println(x) printf("%lld\n", x);
typedef vector<int> vi;
typedef pair<int, int> pii;
const int maxn = 2e6 + 7;
const int maxm = 5e3 + 7;
const int MOD = 1e9 + 7;
const int mod = 998244353;
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int input() {
int x = 0, f = 0;
char ch = 0;
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
inline void print(int x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
const int N = 1e5 + 5;
int n, m, k;
int a[N], b[N];
int f[N];
struct node {
int val;
node* left;
node* right;
};
vector<int> g[N];
vi ans;
void mindfs(int x) {
if (x == -1) return;
mindfs(g[x][0]);
ans.push_back(x);
mindfs(g[x][1]);
}
void work() {
int n, m, k;
n = input();
For(i, 0, n - 1) {
int x, y;
x = input();
y = input();
g[i].push_back(x);
g[i].push_back(y);
}
For(i, 0, n - 1) { a[i] = input(); }
sort(a, a + n);
mindfs(0);
map<int, int> mp;
For(i, 0, ans.size() - 1) {
mp[ans[i]] = a[i];
// print(i);
// puts("");
}
queue<int> q;
q.push(0);
int fir = 0;
while (q.size()) {
if (fir) printf(" ");
fir = 1;
int cur = q.front();
q.pop();
print(mp[cur]);
if (g[cur][0] != -1) q.push(g[cur][0]);
if (g[cur][1] != -1) q.push(g[cur][1]);
}
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#endif
work();
}