Complete Binary Search Tree
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 (≤). 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:
Sample Output:



#include <cstdio>
#include <math.h>
#include <algorithm> using namespace std; int a[];
int T[]; int GetLeftLength(int n)
{
int H=log(n+)/log();
int X=n+-pow(,H);
if(X>pow(,H-))
X=pow(,H-);
int L=pow(,H-)-+X;
return L;
} void solve(int ALeft,int ARight,int TRoot)
{
int n=ARight-ALeft+;
if(n==) return;
int L=GetLeftLength(n);
T[TRoot]=a[ALeft+L];
int LeftTRoot=TRoot*+;
int RightTRoot=LeftTRoot+;
solve(ALeft,ALeft+L-,LeftTRoot);
solve(ALeft+L+,ARight,RightTRoot);
} int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
solve(,n-,);
for(int i=;i<n;i++)
{
if(i==)
printf("%d",T[i]);
else printf(" %d",T[i]);
}
return ;
}

Complete Binary Search Tree的更多相关文章
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 04-树5 Complete Binary Search Tree
这题也是第二次做,本想第一次做时参考的算法会和老师讲的一样,不想老师讲的算法用在这题感觉还不如思雪园友的算法(http://www.cnblogs.com/sixue/archive/2015/04. ...
- 04-树6 Complete Binary Search Tree
完全二叉树 刚开始只发现了中序遍历是从小到大顺序的.一直在找完全二叉树的层结点间规律...放弃了 不曾想,完全二叉树的规律早就知道啊.根结点为i,其左孩子结点2*i, 右孩子结点2*i+1. 结合此两 ...
- A1064. Complete Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 04-树6 Complete Binary Search Tree(30 分)
title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...
- PAT 1064 Complete Binary Search Tree[二叉树][难]
1064 Complete Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a b ...
- PAT 甲级 1064 Complete Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...
- 04-树6 Complete Binary Search Tree (30 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 04-树6 Complete Binary Search Tree (30 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- JavaScript作用域[[scope]]
[[scope]] : 隐式的属性 每个JavaScript函数都是一个对象,对象中有些属性可以访问,而有些属性是不可以访问的,这些属性仅供JavaScript引擎存取, [[scope]]就是其中一 ...
- 2018-4-12 数学建模MATLAB常用的一些函数
一.求函数的极限问题 limit(fun,x,y,str) 意义:fun为所求极限的函数,x代表变量,y代表变量的极限值,str代表这个极限的类型,常用的参数是right,left. 如果是多个变量的 ...
- Hive的初识
Hive是构建于Hadoop上的数据仓库基础框架,它提供了以下功能: 可通过SQL轻松的访问数据,从而实现数据仓库的任务.如提取/转换/加载,报告和数据分析. 对各种数据格式施加结构. 访问存储在HD ...
- pymysql操作mysql的脚本示例
#!/usr/bin/env python#-*- coding:UTF-8 -*- from multiprocessing import Process , Queuefrom queue imp ...
- 尝鲜svnup
最近有同事折腾了一下svnup的编译,终于可以在Mac OS X和Linux上面编译通过了,仓库在这里:https://github.com/lvzixun/svnup/ svnup这个工具只有一个功 ...
- 04bootstrap_表单
03bootstrap_表单 表单的基本实例 1.默认表单:form 表单域 fieldset legend label 提示span class="help-block" 2.基 ...
- echarts立体效果地图-自定义区域及文字
setgeomap: function (_id, _fn) { // 全城拥堵占比 GLOBAL.myMapChart = echarts.init(document.getElementById( ...
- HTML5网页制作好好玩啊
---恢复内容开始--- 这两天在看HTML5,由于学习的需要,所以要学的,嗯,这个整人还是很有意思的(但是超超是好人,从不干坏事) 现在请欣赏一下我的代码和图片吧!(想整人的小伙伴可以自己copy来 ...
- Spring ConditionalOnProperty
Spring Annotation @ConditionalOnProperty spring doc解释 @Conditional: Indicates that a component is on ...
- C++报错
编译的时候会报如下错误:warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable ...