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 properties: The lef 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 lef 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 lef 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 (<=1000). 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
题目分析
已知完全二叉查找树的序列,求其层序序列
解题思路
思路 01
- 输入测试用例,升序排序即为二叉查找树的中序序列
- 中序序列递归建树(存储与数组i节点的子节点为2*i,2*i+1,root在1位置)(模拟递归中序遍历过程)
- 顺序打印数组即为层序序列
思路 02
- 输入测试用例,升序排序即为二叉查找树的中序序列
- 中序序列递归建树(存储与数组i节点的子节点为2i,2i+1,root在1位置)(找root的坐标k,将中序序列划分为startk-1左子树和k+1end右子树)
- 顺序打印数组即为层序序列
知识点
完全二叉查找树,任意节点序列建树
注:二叉查找树(不一定要完全二叉树)任意节点序列,升序排序,即为二叉查找树的中序序列
- 方式一:模拟递归打印中序序列的过程,将中序序列依次插入到数组建树
- 方式二:找root在中序序列中的位置k并将root保存到数组,递归处理startk-1左子树和k+1end右子树,完成建树
void inOrder(int root) { //root保存在1位置,index初始化为0
if(root>n) return;
inOrder(root*2);
CBT[root]=number[index++];
inOrder(root*2+1);
}
void getLevel(int start, int end, int index) {
if(start>end)return;
int n=end-start+1;
int l=log(n+1)/log(2);//最后一层的层数
int leave=n-(pow(2,l)-1); //最后一层叶子结点数
//pow(2, l - 1) - 1是除了root结点所在层和最后?层外,左?树的结点个数,pow(2, l - 1) 是l+1
//层最多拥有的属于根结点左?树的结点个数,min(pow(2, l - 1), leave)是最后?个结点真正拥有的
//属于根结点左?树上的结点个数
int root = start+(pow(2,l-1)-1)+min((int)pow(2,l-1),leave);
level[index]=in[root];
getLevel(start,root-1,2*index+1);
getLevel(root+1,end,2*index+2);
}
Code
Code 01
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1000;
int n,m;
int number[maxn],CBT[maxn],index=0;
void inOrder(int root) {
if(root>n) {
return;
}
inOrder(root*2);
CBT[root]=number[index++];
inOrder(root*2+1);
}
int main(int argc,char * argv[]) {
scanf("%d",&n);
for(int i=0; i<n; i++) {
scanf("%d",&m);
number[i]=m;
}
// 递增排序--二叉查找树中序序列
sort(number,number+n);
inOrder(1);//根节点在1位置
for(int i=1; i<=n;i++) {
if(i!=1)printf(" ");
printf("%d",CBT[i]);
}
return 0;
}
Code 02
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
vector<int> in,level;
void getLevel(int start, int end, int index) {
if(start>end)return;
int n=end-start+1;
int l=log(n+1)/log(2);//最后一层的层数
int leave=n-(pow(2,l)-1); //最后一层叶子结点数
//pow(2, l - 1) - 1是除了root结点所在层和最后?层外,左?树的结点个数,pow(2, l - 1) 是l+1
//层最多拥有的属于根结点左?树的结点个数,min(pow(2, l - 1), leave)是最后?个结点真正拥有的
//属于根结点左?树上的结点个数
int root = start+(pow(2,l-1)-1)+min((int)pow(2,l-1),leave);
level[index]=in[root];
getLevel(start,root-1,2*index+1);
getLevel(root+1,end,2*index+2);
}
int main(int argc,char * argv[]) {
int n;
scanf("%d",&n);
in.resize(n);
level.resize(n);
for(int i=0; i<n; i++) {
scanf("%d",&in[i]);
}
sort(in.begin(),in.end());
getLevel(0,n-1,0);
printf("%d",level[0]);
for(int i=1; i<n; i++) {
printf(" %d",level[i]);
}
return 0;
}
PAT Advanced 1064 Complete Binary Search Tree (30) [⼆叉查找树BST]的更多相关文章
- PAT Advanced 1099 Build A Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- 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 Advanced 1043 Is It a Binary Search Tree (25) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...
- 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 ...
- pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binar ...
- PAT 甲级 1064 Complete Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...
随机推荐
- [题解] LuoguP2257 YY的GCD
传送门 给\(n,m\),让你求 \[ \sum\limits_{i=1}^n \sum\limits_{j=1}^m [\gcd(i,j) \in prime] \] 有\(T\)组询问\((T \ ...
- 腾讯X5内核使用详解(X5内核播放器使用如何去除控制栏全屏播放)以及一些注意事项
例子下载地址 https://www.lanzous.com/i2zsv5g GIT就不用了麻烦的不行 本人安卓刚学 就上X5内核弄了老长时间由于对maven 和idea不熟悉刚开始导包都是 ...
- Java 用户输入
章节 Java 基础 Java 简介 Java 环境搭建 Java 基本语法 Java 注释 Java 变量 Java 数据类型 Java 字符串 Java 类型转换 Java 运算符 Java 字符 ...
- MongoDB 初始化数据同步
MongoDB初始化数据同步: 副本集中的成员启动之后,就会检查自身的状态,确定是否可以从某个成员那里进行同步.如果不行的话,尝试从其他成员那里进行完整的数据复制. 这个过程就是初始化同步(initi ...
- 洛谷 三月月赛 A
模拟模拟 #include<bits/stdc++.h> using namespace std; inline int ra() { ,f=; char ch=getchar(); ; ...
- Python 中 对logging 模块进行封装,记录bug日志、日志等级
是程序产生的日志 程序员自定义设置的 收集器和渠道级别那个高就以那个级别输出 日志和报告的作用: 报告的重点在于执行结果(执行成功失败,多少用例覆盖),返回结果 日志的重点在执行过程当中,异常点,哪里 ...
- 139-PHP static后期静态绑定(二)
<?php class test{ //创建test类 public function __construct(){ static::getinfo(); //后期静态绑定 } public s ...
- 4. Linux 集群安装
1. 配置yum yum clean all yum makecache yum install wget 2.安装JDK (1) 将jdk-7u60-linux-i586.rpm通过WinSCP上传 ...
- VM ESXi虚拟化使用学习笔记
由于疫情原因,没有条件介绍安装部分的内容,也没有安装部分内容的相关截图,所以安装部分可以选择网上资料.但是只要熟练安装CentOS系统的,基本安装ESXi一看就会,设置主机地址方面有一定图形化界面,比 ...
- RobotFramwork中实现oracle数据库的连接(终于解决cx_oracle问题)
RobotFramework连接数据库有java实现,也有python实现的,现在研究的python实现的,java还没尝试.有兴趣的参考后面的转载文章. 步骤(也转载了别人的文章,因为写的比 ...