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. Charles抓包遇到的问题

    1.手机设置了代理但是连不上网,无法下载HTTPS证书,关闭电脑防火墙! 2.content乱码解决方案参考https://www.cnblogs.com/puresoul/p/7365761.htm ...

  2. random模块(随机数)

    random.random() #0-1之间的随机数 random.randint(1,10) #1-10 包括10的随机数 --> int random.choice(list) #随机选取列 ...

  3. http error: "request body stream exhausted"

    'request body stream exhausted' after authentication challenge #661  Closed aburgel opened this issu ...

  4. google搜索引擎使用

    部分引用 http://yearslater.me/2017/06/15/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%E6%90%9C%E7%B4%A2%E5%BC%95 ...

  5. 聊一聊 redux 异步流之 redux-saga

    让我惊讶的是,redux-saga 的作者竟然是一名金融出身的在一家房地产公司工作的员工(让我想到了阮老师...),但是他对写代码有着非常浓厚的热忱,喜欢学习和挑战新的事物,并探索新的想法.恩,牛逼的 ...

  6. Python学习之路基础篇--06Python基础+dict的学习

    字典和列表其实差不多,所说3.6 之前的版本字典是没有顺序的.是Python中唯一的映射数据类型,key 是可哈希(不可变数据类型),如元组.bool.int 和 str:value 是任意数据类型. ...

  7. 关于ubuntu远程访问服务器的知识点

    为了可以打开图形界面,用ssh -X 用户名@地址 如何远程打开文件夹可视化界面 nautilus /文件夹

  8. Spring Boot 框架的依赖管理

    Spring Boot为完成不同需求的Spring应用构建,提供了多种不同的依赖管理模板,每种模板均为一系列已完成的依赖的管理.例如在我们的入门程序中,需要构建web项目,我们只需添加spring-b ...

  9. Dart 创建Map

    Map的常用操作 1. [] , length   获取值和长度 2. isEmpty (),isNoEmpty () 是否为空 3.Keys ,values  获取所有的键 和值 4. contai ...

  10. Linux防火墙开启关闭查询

    1.centos7防火墙 命令含义: –zone #作用域 –add-port=80/tcp #添加端口,格式为:端口/通讯协议 –permanent #永久生效,没有此参数重启后失效 服务与端口的启 ...