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

通过观察可以注意到,对完全二叉树当中的任何一个结点(设编号为x),其左孩子的编号一定是2x,而右孩子的编号一定是2x + 1。也就是说,完全二叉树可以通过建立一个大小为2k的数组来存放所有结点的信息,其中k为完全二叉树的最大高度,且1号位存放的必须是根结点(想一想为什么根结点不能存在下标为0处?)。这样就可以用数组的下标来表图95完全二又树编号示意示结点编号,且左孩子和右孩子的编号都可以直接计算得到。
事实上,如果不是完全二叉树,也可以视其为完全二叉树,即把空结点也进行实际的编号工作。但是这样做会使整棵树是一条链时的空间消耗巨大(对k个结点就需要大小为2k的数组),因此很少采用这种方法来存放一般性质的树。不过如果题目中已经规定是完全二叉树,那么数组大小只需要设为结点上限个数加1即可,这将会大大节省编码复杂度。
除此之外,该数组中元素存放的顺序恰好为该完全二叉树的层序遍历序列。而判断某个结点是否为叶结点的标志为:该结点(记下标为root)的左子结点的编号root * 2大于结点总个数n(想一想为什么不需要判断右子结点?);判断某个结点是否为空结点的标志为:该结点下标 root大于结点总个数n。

 #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, nums[], res[], index = ;
void levelOrder(int k)
{
if (k > N)//叶子节点
return;
levelOrder(k * );//遍历左子树
res[k] = nums[index++];//即遍历完左子树后,此时即为根节点
levelOrder(k * + );//遍历右子树
}
int main()
{
cin >> N;
for (int i = ; i < N; ++i)
cin >> nums[i];
sort(nums, nums + N, [](int a, int b) {return a < b; });
levelOrder();
for (int i = ; i <= N; ++i)
cout << res[i] << (i == N ? "" : " ");
return ;
}

PAT甲级——A1064 Complete Binary Search Tree的更多相关文章

  1. PAT 甲级 1064 Complete Binary Search Tree

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

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

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

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

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

  4. pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)

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

  5. A1064. Complete Binary Search Tree

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

  6. PAT Advanced 1064 Complete Binary Search Tree (30) [⼆叉查找树BST]

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

  7. PAT_A1064#Complete Binary Search Tree

    Source: PAT A1064 Complete Binary Search Tree (30 分) Description: A Binary Search Tree (BST) is recu ...

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

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

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

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

随机推荐

  1. id(), is, ==, 的区别与小数据池

    1. id() 内存地址 s = 'asdf' n = id(s) print(n)输出:16506464 #16506464为变量s的内存地址 2. == 比较数值 3. is 比较内存地址 数字, ...

  2. windows api(GDI)实现图片旋转

    GDI实现图片旋转,博主在网上找了好多资料,都不太如意. 并且在尝试中发现,如果先用SetViewportOrgEx将HDC上的坐标原点移动到图片中心:再在HDC上的获取每个点,用三角函数进行变换,算 ...

  3. angluar1.8.2 PC Mail项目笔记

    兼容性技术选型 前后端分离 代理gulp nginx jq+angluar1.8.2 使用级别刚刚好的相对目录,方便转移项目或者做接口代理时的切换目录 指令过滤器服务控制器书写位置 方法封装,自己写和 ...

  4. PAT甲级——A1133 Splitting A Linked List【25】

    Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...

  5. java中 ++a 与 a++ 的区别

    public static void main(String[] args) { int a = 5; a ++; System.out.println(a); int b = 5; ++ b; Sy ...

  6. 《我是一只IT小小鸟》读书笔记 PB16110698 第四周(~3.29)

    <我是一只IT小小鸟>读书笔记 本周在邓老师的推荐下,我阅读了<我是一只IT小小鸟>,这本书由21位初入职场的IT人的传记组成,记录了他们成长道路上的酸甜苦辣.书中一段段鲜活生 ...

  7. POJ1160 Post Office-四边形不等式优化DP

    方程 $\Large f(i,j)=min(f(i-1,k)+w(k+1,j))$ 其中$w(i,j)$表示在$[i,j]$的村庄都去一个邮局的最小距离和 证明w满足四边形不等式 设$w_k(i,j) ...

  8. 常见的React面试题

    1.redux中间件 答:中间件提供第三方插件的模式,自定义拦截 action -> reducer 的过程.变为 action -> middlewares -> reducer ...

  9. Python操作MySQL以及数据库索引

    目录 python操作MySQL 安装 使用 SQL注入问题 MySQL的索引 为什么使用索引 索引的种类 主键索引 唯一索引 普通索引 索引优缺点 不会命中索引的情况 explain 索引覆盖 My ...

  10. SpringData_04_ JPA中的一对多

    1.JPA中的一对多 在一对多关系中,我们习惯把一的一方称之为主表,把多的一方称之为从表.在数据库中建立一对多的关系,需要使用数据库的外键约束. 什么是外键? 指的是从表中有一列,取值参照主表的主键, ...