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 ...
随机推荐
- eclipse+terminal
eclipse 怎么安装terminal插件 1 首先打开eclipse,找到help菜单,点击Eclipse Marketplace. 2 在search框里输入Terminal,点击Go查找. ...
- day16_函数作用域_匿名函数_函数式编程_map_reduce_filter_(部分)内置函数
20180729 补充部分代码 20180727 上传代码 #!/usr/bin/env python # -*- coding:utf-8 -*- # ***************** ...
- JDK9新特性实战:简化流关闭新姿势。
做Java开发的都知道,每个资源的打开都需要对应的关闭操作,不然就会使资源一直占用而造成资源浪费,从而降低系统性能. 关于资源的关闭操作,从JDK7-JDK9有了不少的提升及简化. JDK6 在JDK ...
- JS时间比较大小
目录 1. 时间比较 2. 时间戳比较 3. 日期比较方法 4. 参考 1. 时间比较 var curTime = new Date(); //把字符串格式转化为日期类 var starttime = ...
- 9.SpringJDBC模板类
1. Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单 2. 提供了JDBC模板,Spring框架提供的 * JdbcTemplate类 3. Spring框架可以整 ...
- python spark环境配置
在配置Hadoop之前,应该先做以下配置 1.更改主机名 首先更改主机名,目的是为了方便管理. 输入:hostname 查看本机的名称 使用 hostname 修改当前主 ...
- Loadrunner系列学习--Loadrunner架构(1)
Loadrunner系列学习--Loadrunner架构(1) 最近在学习Loadrunner,发现一个英文网站http://www.wilsonmar.com/1loadrun.htm里面介绍的比较 ...
- LR回放webservice脚本报错------------mmdrv.exe应用程序错误(未解决)
1.录制完成webservice脚本如下: 2.回放脚本,报错: 3.网上查看了一些解决办法,但都是没有起到作用.
- CF1097E Egor and an RPG game
最少反链划分数 = 最长链.实现:每次找出所有极大元作为一个反链. 任意长度小于k * (k + 1) / 2的排列都能被划分为不多于k个单调序列.且这是一个紧的上界. 然后这题就可以切了. 题意:给 ...
- 19-11-08-Night
再咕咕咕会被爆捶吗??? ZJ: 喜闻乐见: 27 Miemeng 60 01:59:43 100 01:59:44 0 01:59:44 160 01:59:44 最水的$T1$挂了???? $T2 ...