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:

10
1 2 3 4 5 6 7 8 9 0

Sample Output:

6 3 8 1 5 7 9 0 2 4
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ; int num[maxn];
int CBT[maxn];
int index = ; void inOrder(int root, int n); int main()
{
int n;
scanf("%d",&n);
for (int i = ; i < n; i++)
{
scanf("%d",&num[i]);
}
sort(num,num+n); inOrder(,n);
for (int i = ; i <= n; i++)
{
printf("%d",CBT[i]);
if (i < n)
{
printf(" ");
}
} return ;
} void inOrder(int root, int n)
{
if (root > n)
{
return ;
} inOrder(root*, n);
CBT[root] = num[index++];
inOrder(root*+, n);
}

04-树6 Complete Binary Search Tree (30 分)的更多相关文章

  1. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

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

  2. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

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

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree   (30分) A ...

  4. 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)

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

  5. 【PAT甲级】1064 Complete Binary Search Tree (30 分)

    题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...

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

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

  7. pat04-树8. Complete Binary Search Tree (30)

    04-树8. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHE ...

  8. pat1064. Complete Binary Search Tree (30)

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

  9. pat 甲级 1064. Complete Binary Search Tree (30)

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

随机推荐

  1. framework7 总结之前遇到的问题和踩过的坑

    官网上写的案例比较简单明了,我这里就将我使用时踩过的坑做一个总结,与大家共勉! 最近使用framework,基本全靠浏览官方文档,当然,有遇到了许多的错误,开始不知道哪里出问题也很着急,到最后发现问题 ...

  2. mysql 5.7 修改root密码允许远程连接

    1.修改root密码(其他用户类似)  试过网上看的一些 在mysql数据库执行 update user set password='新密码'  where user='root' 执行说找不到字段, ...

  3. Oracle使用中的常规操作总结

    写一篇在使用Oracle过程中一些常用的操作,以便于忘记的时候查看 一.创建用户和给用户赋予权限 create user 用户名 identified by 密码; --12c一下版本 create ...

  4. 接口例_龟车赛跑_Java

    此例演示java中接口的一般用法. 屋子里有一群程序员,每个人在写着自己的类,共同构建一个世界. 项目经理突然出现:“打扰大家一下,公司决定举办一个竞速比赛,你们写的类都可以参加.为了比赛的顺利进行, ...

  5. 异常---Day21(写得有错请指出,感谢)

    异常的概念 异常,就是不正常的意思.在生活中:医生说,你的身体某个部位有异常,该部位和正常相比有点不同,该部位的功能将受影响.在程序中的意思就是:指的是程序在执行过程中,出现的非正常的情况,终会导致J ...

  6. HTTP发展简史

    HTTP发展简史 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的 ...

  7. Vue学习之基础及部分指令小结(一)

    一.理解MVC和MVVM的关系: MVC:Model View Controller (模型 视图 控制器) 分别为:业务逻辑.界面.用来调度View和Model层 MVVM:Model View V ...

  8. 原生aJax跨域

    今天遇到一个问题,利用原生ajax请求时报了一段错, 大概意思就是同源策略阻止了访问,也就是跨域, 后来发现是我在代码中发送了请求头, xhr.setRequestHeader(); 将这段代码删除后 ...

  9. 微信小程序分页加载列表

    1.假设加载的数据为 2.wxml <view class="page"> <view class="page__bd"> <vi ...

  10. 4 Android可执行文件

    APK是Android Package缩写,使用zip解压文件即可打开.每个APK文件中都包含一个class.dex文件(odex过的APK文件除外).class.dex文件就是Android系统Da ...