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. win 7 下安装GIT(亲测有效)

    我首先是百度到了这个网站:https://git-scm.com/download/win 当然由于外网访问速度的缓慢 可以直接在百度搜索下载自己对应的版本 这个网站上有下载链接,你可以根据你的系统选 ...

  2. ORM(Object/Relation Mapping)框架简介

    ORM 框架简介 对象-关系映射(Object/Relation Mapping,简称ORM),是随着面向对象的软件开发方法发展而产生的.面向对象的开发方法是当今企业级应用开发环境中的主流开发方法,关 ...

  3. PAT甲级——A1109 Group Photo【25】

    Formation is very important when taking a group photo. Given the rules of forming K rows with Npeopl ...

  4. PostMan授权认证使用

    Authorization 对于很多应用,出于安全考虑我们的接口并不希望对外公开.这个时候就需要使用授权(Authorization)机制. 授权过程验证您是否具有访问服务器所需数据的权限. 当发送请 ...

  5. POJ - 2774~POJ - 3415 后缀数组求解公共字串问题

    POJ - 2774: 题意: 求解A,B串的最长公共字串 (摘自罗穗骞的国家集训队论文): 算法分析: 字符串的任何一个子串都是这个字符串的某个后缀的前缀. 求 A 和 B 的最长 公共子串等价于求 ...

  6. 计算图像数据集的RGB均值

    最近在跑代码的时候,需要用到RGB三个通道上的均值,如下图所示: 写了一个程序,如下: import os import cv2 import random import numpy as np #数 ...

  7. 9个永恒的UI设计原则

    很多人都在寻找那些能够帮助他们快速提升设计能力的方法,但你是否想过,自己身上的哪些方面会对你的设计产生影响呢?是使用工具的技巧,对设计的理解和态度,还是你的生活习惯呢?我想说所有这些都是决定你的设计是 ...

  8. Ubuntu环境下java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

    一.问题描述 Ubuntu环境中,将maven项目打包成jar包传到服务器中后运行以下指令: $ java -cp my.jar com.myproject.Main 遇到报错: java.lang. ...

  9. Oracle 从 dual 表中查询返回多行记录

    同时查询出十条数据 ; 按照这个特性计算两个日期之间的工作日: select days, week as days, to_char(to_date(, 'day') as week from dua ...

  10. Luogu P2619 [国家集训队2]Tree I(WQS二分+最小生成树)

    P2619 [国家集训队2]Tree I 题意 题目描述 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有\(need\)条白色边的生成树. 题目保证有解. 输入输出格式 输入格式 ...