PAT甲级——A1064 Complete Binary Search Tree
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的更多相关文章
- PAT 甲级 1064 Complete Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binar ...
- A1064. Complete Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 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 ...
- PAT_A1064#Complete Binary Search Tree
Source: PAT A1064 Complete Binary Search Tree (30 分) Description: A Binary Search Tree (BST) is recu ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
随机推荐
- Invalid argument value:无效参数值。原因是:把Session值user0当做username作为参数了。 而实际上此时username是user0的成员变量。参数应该是user0.getUsername();然后发现别人的List得加泛型,我的怎么不用加,运行报错,上网收了错误原因,因为导包错误,不小心导错包了,改为util.List包对了。
- 数据库连接JDBC
#=======================mysql============================= #jdbc.driverClassName=com.mysql.jdbc.Driv ...
- scrpy--分布式爬虫
原来的scrapy中的Scheduler维护的是当前机器中的任务队列(存放着Request对象以及回调函数等信息) + 当前的去重队列(存放访问过的url地址) 实现分布式的关键就是需要找一台专门的主 ...
- (function($){….})(jQuery)与$(function(){})的区别
function fun($){…};fun(jQuery);这种方法多用于存放开发的插件,执行其中的代码时,Dom对象并不一定加载完毕. $(function(){})等价于$(document). ...
- java笔试之尼科彻斯定理
验证尼科彻斯定理,即:任何一个整数m的立方都可以写成m个连续奇数之和. 例如: 1^3=1 2^3=3+5 3^3=7+9+11 4^3=13+15+17+19 这题也可以用数学公式推理,首项m*(m ...
- scull 的内存使用
scull 使用的内存区, 也称为一个设备, 长度可变. 你写的越多, 它增长越多; 通过使用 一个短文件覆盖设备来进行修整. scull 驱动引入 2 个核心函数来管理 Linux 内核中的内存. ...
- minutia cylinder code MCC lSSR 匹配算法
图一 是LSS匹配算法, 图二是LSSR 匹配算法,数据采用MCC SDK自带的十个人的数据.LSS EER6.0%左右,LSSR EER 0%
- 四种JavaEE架构简介
1. 传统三层架构 配图是一个基于MVC的三层架构, 大致可以分成表现层, 业务层和持久层 表现层负责接收请求和转发请求 业务层主要负责处理请求, 值得注意的是事务管理, 日志记录等操作通常也是封装在 ...
- 廖雪峰Java15JDBC编程-2SQL入门-1SQL介绍
1.SQL:结构化查询语言 Structured Query Language 针对关系数据库设计 各种数据库基本一致 允许用户通过SQL查询数据而不关心数据库底层存储结构 1.1 SQL使用: 可以 ...
- COMMENT方法 用于在生成的SQL语句中添加注释内容,
COMMENT方法 用于在生成的SQL语句中添加注释内容,例如: $this->comment('查询考试前十名分数') ->field('username,score') ->li ...