[PAT]1064 Complete 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.
A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.
Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.
Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.
Sample Input:
10
1 2 3 4 5 6 7 8 9 0
Sample Output:
6 3 8 1 5 7 9 0 2 4
二、题解
题意:输入一串数字,要求将其构成一个二叉搜索树,并且也是完全二叉树。最后输出树的层次遍历。
二叉搜索树需要满足任一节点的左孩子都小于自己,右孩子都大于或者等于自己。完全二叉树需要满足,除最后一层外,二叉树的所有层都必须是满的,即除了倒数第1层(叶节点)、倒数第2层外,所有的节点都有两个孩子。
分析:首先对这串数字从小到大排序,然后使用递归建树,建完树后再层次遍历。
在建树的时候,维持两个变量:l、r,记录建树所需要的数字在已知串中的下表,然后找到树的根节点所在的下标mid,新建节点。然后再用下标[l,mid-1]之间的数字建左子树,[mid+1,r]之间的数字建右子树。
最后层次遍历。
AC代码:
#include <bits/stdc++.h>
using namespace std;
struct node {
int val;
node* left;
node* right;
node(int val, node* left = NULL, node* right = NULL) {
this->val = val;
this->left = left;
this->right = right;
}
};
vector<int> nums;
node* createTree(int l, int r) {
if(l>r){
return NULL;
}
if(l==r) {
node* tempnode = new node(nums[l]);
return tempnode;
} else {
//一共n个
int n = r-l+1;
//找几层
int level = 0;
for(int i=1; ; i++) {
if((int)pow(2, i)-1<=n && pow(2,i+1)-1 > n) {
level = i;
break;
}
}
int buttom = n-((int)pow(2, level)-1);//最下层的数量
int but = pow(2, level);//最下层最多的个数
//最下层左右的数量
int butleft = buttom>=but/2 ? but/2 : buttom;
int butright = buttom>=but/2? buttom-(but/2) : 0;
int top = n - buttom;//上层的数量
int halftop = (top-1)/2;
//middle的下标是:l+左子树的数量
int midindex = l+butleft+halftop;
node* tempnode = new node(nums[midindex]);
tempnode->left = createTree(l, midindex-1);
tempnode->right = createTree(midindex+1, r);
return tempnode;
}
}
int main() {
int n;
cin>>n;
nums.resize(n);
for(int i=0; i<n; i++) {
int temp;
cin>>temp;
nums[i] = temp;
}
sort(nums.begin(), nums.end());
node* root = createTree(0, n-1);
vector<int> ans;
queue<node*> q;
q.push(root);
while(!q.empty()) {
ans.push_back(q.front()->val);
if(q.front()->left!=NULL) {
q.push(q.front()->left);
}
if(q.front()->right!=NULL) {
q.push(q.front()->right);
}
q.pop();
}
for(int i=0; i<ans.size()-1; i++) {
cout<<ans[i]<<" ";
}
cout<<ans.back()<<endl;
return 0;
}