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. C# receive 接收的字符串 对比不相等的问题

    背景: 最近写了一个C/S架构的 C# Socket 通讯项目,服务端,客户端弄通了之后,客户端发送给服务端后,服务端回复一个字符串告诉客户端确认收到了.问题就在这. 问题: 一开始,我让服务端回复了 ...

  2. python基础---面向对象的概念

    1.面向对象 什么是面向过程?? 将一个复杂单位问题一步步小化,最终只需要完成一个人小的功能就可以了 比如:将大象放进冰箱要几步? 一共三步:打开冰箱,把大象塞进入,关门就可以了 优点:复杂度降低了, ...

  3. Django models 的字段类型

    1.models.AutoField   ---自增列 = int(11)    如果没有的话,默认会生成一个名称为 id 的列,如果要显示的自定义一个自增列,必须将给列设置为主键 primary_k ...

  4. Hive与HBase表联合使用Join的问题

    hive与hbase表结合级联查询的问题,主要hive两个表以上涉及到join操作,就会长时间卡住,查询日志也不报错,也不会出现mr的进度百分比显示,shell显示如下图 如图: 解决这个问题,需要修 ...

  5. 开发Canvas 绘画应用(二):实现绘画

    开发Canvas 绘画应用(一):搭好框架 中我们已经把基本框架及基础功能实现了,现在要实现本应用的重点:绘画功能. 首先分析一下,我们要实现绘画,需要具备的理论知识如下: (1)获取触摸点的坐标 类 ...

  6. 测试那些事儿-软测必备的linux知识(五)

    1.进程管理 1.1进程概述 在Linux中,每个执行的程序都称为一个进程,每个进程都分配一个ID号 每个进程,都会对应一个父进程,这个父进程可以复制多个子进程, 每个进程可能以两种方式存在,前台与后 ...

  7. 用雷达统计成绩单、numpy、matplotlib的使用

    #e19.1DrawRadar import numpy as np import matplotlib.pyplot as plt import matplotlib matplotlib.rcPa ...

  8. 《DSP using MATLAB》Problem 7.14

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  9. Windows L2TP Client Setup

    原文链接:http://www.softether.org/4-docs/2-howto/9.L2TPIPsec_Setup_Guide_for_SoftEther_VPN_Server/4.Wind ...

  10. c#函数地址传入c++

    c# private delegate void ValidateEvent(int eventCode); private static void ValidateEventCallback(int ...