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>
using namespace std;
typedef struct node{
int left,right,val;
}node;
node tree[105];
int countNodeNum(int node_id){
if(node_id == -1)
return 0;
else
return 1 + countNodeNum(tree[node_id].left) + countNodeNum(tree[node_id].right);
}
void buildtree(vector<int>& num, int root_index){
int left_node_num = countNodeNum(tree[root_index].left);
vector<int> leftNum, rightNum;
for(int i = 0;i < left_node_num; i++)
leftNum.push_back(num[i]);
tree[root_index].val = num[left_node_num];
for(int i = left_node_num+1;i < num.size(); i++)
rightNum.push_back(num[i]);
if(leftNum.size() != 0)
buildtree(leftNum, tree[root_index].left);
if(rightNum.size() != 0)
buildtree(rightNum, tree[root_index].right);
}
int main(int argc, char const *argv[])
{
int n, L, R, t;
cin >> n;
for(int i = 0;i < n; i++){
cin >> L >> R;
tree[i].left = L;
tree[i].right = R;
tree[i].val = 0;
}
vector<int> num;
for(int i = 0;i < n; i++){
cin >> t;
num.push_back(t);
}
sort(num.begin(), num.end());
// num 数组包含的是树中的节点的值 第二个参数为节点标号
buildtree(num, 0);
queue<int> q;
q.push(0);
vector<int> ans;
while(!q.empty()){
int v = q.front();q.pop();
if(v != -1){
ans.push_back(tree[v].val);
q.push(tree[v].left);
q.push(tree[v].right);
}
}
for(int i = 0;i < ans.size(); i++){
cout << ans[i];
if(i != ans.size()-1)
cout << " ";
}
return 0;
}