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
结尾无空行
题目:
给定一个序列,这个序列可以构造一棵唯一的树,这棵树同时是完全二叉树和二叉搜索树,要求输出二叉搜索树的层次遍历
输入:
节点个数
节点值
这一题求完全二叉树的二叉搜索树的层次遍历
二叉搜索树的特性在于当你使用中序遍历时,序列是一个升序序列
完全二叉树的特性在于左子结点所在位置为其父节点的两倍,即左子节点_LOC = 父节点_LOC * 2;右子节点则为父节点的两倍加1,即右子节点_LOC = 父节点_LOC * 2 + 1。(LOC从1开始)
根据上述特性,将例子画成二叉树形式:(右边是位置,左边是例子序列的值,例如如果要按照例子中的序列形成这样的树,那么左边的0就必须在右边的8的位置)
怎么根据给定的序列画出左边这幅图?
其实是根据它可以形成一颗完全二叉树和二叉搜索树,先根据节点个数画出右边的图,再根据每个根节点的左右子树节点个数画出左图,例如知道1的左子树有6个节点,右子树有3个节点,根据BST的中序序列0 1 2 3 4 5 6 7 8 9(也就是升序),知道左边6个右边3个的唯一节点为6,所以6就是整棵树的根节点
上图中左边是二叉树的形式,右边是从根节点为1开始,一直按顺序排列下来。可以看到右边图从1到10的顺序刚好是层次遍历的顺序。可以看到对左边进行中序遍历,第一个数字即为0,对应LOC为8,他的父节点为8 / 2,右兄弟节点是8 / 2 + 1。
因此这题的做法则是对输入数字进行升序排序得到中序遍历序列,模仿中序遍历的过程,对右边的图进行中序遍历的第一个数字对应层次遍历的相应位置。(这也说明了为什么一开始中序遍历位置是1,因为1对应的是根节点的位置,从1开始才会有上面两倍的规律(0*2 = 0))
思路总结:
1、根据给定的序列画出两棵树
2、此时知道二叉搜索树的中序序列,模拟中序遍历的顺序遍历自己画的完全二叉树,规律为左中右,左节点为父节点两倍,右节点为左节点加1。根据模拟过程得到的序列应该要与给定的升序序列一致,以此得到完全二叉树各个位置的值
3、由于第二幅图完全二叉树我们正是根据层次遍历顺序给定序号,因此上一步得到的结果即为要输出的结果
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> node(1010,0);
vector<int> res(1010,0);
int n;
int k = 0;
void dfs(int root){
if(root > n)return;
dfs(2 * root);
res[root] = node[k++];
dfs(2 * root + 1);
return;
}
int main(){
cin >> n;
for(int i = 0;i < n;++i){
cin >> node[i];
}
sort(node.begin(),node.begin() + n);
dfs(1);
cout << res[1];
for(int i = 2;i <= n;++i){
cout << ' ' << res[i];
}
return 0;
}
题目:
给定一个二叉搜索树的结构和各个节点的值,要求输出二叉搜索树的层次遍历序列
输入:
节点个数
(层数从0开始,为节点索引) 左孩子索引 右孩子索引
节点值
步骤:
1、对给定序列进行升序排序,二叉搜索树的中序序列就是升序序列
2、根据二叉搜索树的结构对这个树模拟中序遍历,建立树。(对每个节点的索引进行分配值)
3、最后再进行一次层次遍历
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
struct node{
int data;
int lchild,rchild;
}tree[110];
int n;
vector<int> que;
int k = 0;
void dfs(int root){
if(tree[root].lchild != -1){
dfs(tree[root].lchild);
}
tree[root].data = que[k++];
if(tree[root].rchild != -1){
dfs(tree[root].rchild);
}
}
void bfs(int root){
queue<int> q;
q.push(root);
int flag = 0;
while(!q.empty()){
int temp = q.front();
q.pop();
if(flag == 0){
cout << tree[temp].data;
flag = 1;
}
else{
cout << ' ' << tree[temp].data;
}
if(tree[temp].lchild != -1){
q.push(tree[temp].lchild);
}
if(tree[temp].rchild != -1){
q.push(tree[temp].rchild);
}
}
}
int main(){
cin >> n;
que.resize(n);
for(int i = 0;i < n;++i){
cin >> tree[i].lchild >> tree[i].rchild;
}
for(int i = 0;i < n;++i){
cin >> que[i];
}
sort(que.begin(),que.begin() + n);
dfs(0);
bfs(0);
return 0;
}