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
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
6 3 8 1 5 7 9 0 2 4
//读入数据排序得到有序数组a[],建立结果树b[]
//计算比root小的数有X个,则root=a[X+1],b[0]=root
//对左序列及右序列分别递归调用函数,将数据读入b中
//输出
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 1000;
int a[N], b[N];
void solve(int abegin, int n, int root);
int main() {
int n;
scanf("%d", &n);
if (n) {
int i;
for (i = 0; i != n; i++)
scanf("%d", &a[i]);
sort(a, a + n);
solve(0, n, 0);
printf("%d", b[0]);
for (i = 1; i != n; i++)
printf(" %d", b[i]);
}
return 0;
}
void solve(int abegin, int n, int root) {
if (!n) return;
int leftn;
double x, h;
//最后一层(设高度h+1)的节点数0<=x<2^h
h = floor(log(n + 1.0) / log(2));//floor(n)返回<=n的最大整数
x = n - (pow(2, h) - 1);
leftn =(int) (x < pow(2, h - 1)) ?
pow(2, h - 1) - 1 + x : pow(2, h - 1) - 1 + pow(2, h - 1);
b[root] = a[abegin + leftn];
solve(abegin, leftn, root * 2 + 1);
solve(abegin + leftn + 1, n - 1 - leftn, (root + 1) * 2);
}
//读入数据排序得到有序数组a[],建立结果树b[]
//对树b执行中序遍历依次填入a中数据
//输出
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 1000;
int a[N], b[N];
void Intraversal(int root, int n);
int main() {
int n;
scanf("%d", &n);
if (n) {
int i;
for (i = 0; i != n; i++)
scanf("%d", &a[i]);
sort(a, a + n);
Intraversal(0, n);
printf("%d", b[0]);
for (i = 1; i != n; i++)
printf(" %d", b[i]);
}
return 0;
}
int k = 0;
void Intraversal(int root, int n) {
if (root >= n) return;
int lr = 2 * root + 1, rr = lr + 1;
Intraversal(lr, n);
b[root] = a[k++];
Intraversal(rr, n);
}