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的更多相关文章

  1. PAT题库-1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  2. 04-树5 Complete Binary Search Tree

    这题也是第二次做,本想第一次做时参考的算法会和老师讲的一样,不想老师讲的算法用在这题感觉还不如思雪园友的算法(http://www.cnblogs.com/sixue/archive/2015/04. ...

  3. 04-树6 Complete Binary Search Tree

    完全二叉树 刚开始只发现了中序遍历是从小到大顺序的.一直在找完全二叉树的层结点间规律...放弃了 不曾想,完全二叉树的规律早就知道啊.根结点为i,其左孩子结点2*i, 右孩子结点2*i+1. 结合此两 ...

  4. A1064. Complete Binary Search Tree

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  5. 04-树6 Complete Binary Search Tree(30 分)

    title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...

  6. PAT 1064 Complete Binary Search Tree[二叉树][难]

    1064 Complete Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a b ...

  7. PAT 甲级 1064 Complete Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...

  8. 04-树6 Complete Binary Search Tree (30 分)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  9. 04-树6 Complete Binary Search Tree (30 分)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

随机推荐

  1. 【EMV L2】Select PSE应用选择相关的卡片数据格式

    The data field of the response message contains the FCI specific to the selected PSE, DDF, or ADF. 一 ...

  2. Vue原理--虚拟DOM

    为什么需要虚拟DOM? 如果对前端工作进行抽象的话,主要就是维护状态和更新视图,而更新视图和维护状态都需要DOM操作.其实近年来,前端的框架主要发展方向就是解放DOM操作的复杂性. 运行js的速度是很 ...

  3. SQLI DUMB SERIES-15

    (1)无论输入什么都没有回显,但是输入 admin'# 时会显示成功登录,说明闭合方式是单引号 (2)无回显,则使用盲注,可用用时间延迟攻击法. 测试时间延迟是否可行: uname=admin' an ...

  4. MATLAB实现Brovey图像融合

    自定义函数: function BF=Brovey_fuse(Hyperspectral_image,High_resolution_image) x0=imread(Hyperspectral_im ...

  5. 小程序cover-view踩过的坑

    1.前段时间开发一个小程序,里边用到cover-view,发现cover-view其实很多都是不支持的,比如动画,如果你想要在cover-view上边加动画你会发现没有效果,虽然在模拟器里边可以显示动 ...

  6. 卷积神经网络 CNN

    卷积神经网络与普通的神经网络十分相似:他们都由神经元构成,这些神经元拥有可学习的权重和偏差.每一个神经元接收一些输入,执行点积运算并以非线性可选择地跟随它.整个网络仍然表征一个单个可微分的分数函数:从 ...

  7. PHP连接mysql数据库报错:Call to undefined function mysql_connect()

    http://php.net/manual/zh/intro.mysqli.php 系统环境PHP7.0+Mysql5.7+Apache2. 运行一个数据库连接测试示例时报错: [client 127 ...

  8. maya权重拷贝一对一,一对多

    是不是有时候盔甲很多很碎头很大,用代理一个个拷贝很麻烦吧 用下面代码直接运行进行一对多或者一对一拷贝 第一个先选择参考物体,加选其他一堆有的没的有权重或没权重的物体执行一键拷贝,就可以啦 以下是源码送 ...

  9. HOMEWORK1

    回顾你过去将近3年的学习经历 当初你报考的时候是真正喜欢计算机这个专业吗? 当初报考的时候是选择英语和计算机专业,报英语那个学校没去上,就来学了计算机,对计算机专业的感觉介于喜欢和热爱之间,就是说还是 ...

  10. 窗函数法设计FIR滤波器参数特征表