PAT 1064 Complete Binary Search Tree[二叉树][难]
1064 Complete Binary Search Tree (30)(30 分)
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 (<=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
题目大意:给出一组数,是一颗完全二叉树并且是二叉搜索树,输出层次遍历序列。
//看到题目感觉很懵,完全没有做过这样的题。
代码来自:https://www.nowcoder.com/questionTerminal/ab6b7429f9c34880837cf4830b2434df
#include<cstdio>
#include<algorithm>
using namespace std;
int node[],tree[];
int N,pos;
void creatTree(int root){//就相当于顺序着把根安排在了tree上。
//递归安排上了之后自然就是层次存储。
if(root>N) return;
int lchild=root*,rchild=root*+;
creatTree(lchild);
tree[root]=node[pos++];
creatTree(rchild);
}
bool cmp(int a,int b){
return a<b;
}
int main(){
scanf("%d",&N);
for(int i=;i<N;i++){
scanf("%d",&node[i]);
}
sort(node,node+N,cmp);//既然是二叉搜索树,那么排序完了之后自然就是中序遍历。
pos=;
creatTree();
for(int i=;i<=N;i++){//注意下标是从1开始计算的。
printf("%d",tree[i]);
if(i<N){
printf(" ");
}
}
}
1.根据完全二叉树在数组中的存储特点,下标的特点。左子树是root*2,右子树是root*2+1.
2.使用pos来记录下标,因为是中序,所以是中序遍历存完全二叉树。
3.使用二叉搜索树的性质,中序遍历即从小到大。
PAT 1064 Complete Binary Search Tree[二叉树][难]的更多相关文章
- PAT 1064 Complete Binary Search Tree
#include <iostream> #include <cstdio> #include <cstdlib> #include <vector> # ...
- 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)
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 (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 ( ...
- 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 ...
随机推荐
- 用layer插件实现tp3.2的分页
主要需要用到 /layer/layer.js 这个, 现在一个tp前端视图/article/index.html <!DOCTYPE html> <html lang=" ...
- sklearn提供的自带的数据集
sklearn 的数据集有好多个种 自带的小数据集(packaged dataset):sklearn.datasets.load_<name> 可在线下载的数据集(Downloaded ...
- 学了Python可以做什么工作
学了Python可以做什么工作 用 Python 写爬虫 据我所知很多初学 Python 的人都是使用它编写爬虫程序.小到抓取一个小黄图网站,大到一个互联网公司的商业应用.通过 Python 入门爬虫 ...
- SpringMVC温故知新
1. SpringMVC流程简记 (1) 用户发送请求至前端控制器DispatcherServlet (2) DispatcherServlet收到请求调用HandlerMapping处理器映射器 ( ...
- centos7和Ubuntu上的关机需要手动关闭电源的问题
author:heandsen chen date: 2018-11-11 20:36:38. # halt 执行后会出现这个问题 解决办法: # init 0 # shutdown -h now ...
- Build step 'Execute Windows batch command' marked build as failure
坑爹的Jenkis,在执行windows命令编译.NET项目的时候命令执行成功了,但是却还是报了这样一个错: Build step 'Execute Windows batch command' ma ...
- 关于Virtual Box虚拟机里的系统不能启动的解决方法
当我们的虚拟机在非正常关闭后,再次启动机器时,Virtual Box会出现 Runtime error opening 'C:\Users\admin\VirtualBox VMs\Windows S ...
- 消息通讯之关于消息队列MQ必须了解的相关概念
目录 系统通讯方式有哪些? 消息队列的应用场景 消息队列通讯模型 常见的消息协议 AMQP MQTT ATOMP JMS 小结 系统通讯方式有哪些? RPC调用 RPC 全称 Remote Proce ...
- list,set中可以存放Object类型对象
List<JSONObject> series = new ArrayList<JSONObject>();
- Spyer中添加一些常用包的方法
我用的是Anaconda中的Spyer编译,在导入包pyaudio时,发现找不到,需要手工导入.可以打开Anacoda promt,查看已经安装的包名用:pip list Spyer中的所有包在这里安 ...