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[二叉树][难]的更多相关文章

  1. PAT 1064 Complete Binary Search Tree

    #include <iostream> #include <cstdio> #include <cstdlib> #include <vector> # ...

  2. 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 ...

  3. pat 甲级 1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  4. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

    1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a bin ...

  5. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  6. PAT题库-1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  7. pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)

    1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binar ...

  8. PAT 甲级 1064 Complete Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...

  9. 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 ...

随机推荐

  1. 文件系统的挂载(2)---挂载rootfs文件系统

    一.目的 本文主要讲述linux内核rootfs文件系统的挂载过程,内核版本为3.10. rootfs是基于内存的文件系统,没有实际的存储设备,所有操作都在内存中完成.为了保证linux内核的精简性, ...

  2. c++ vector详解

    容器有太多需要清楚细节的地方,同时也有太多值得学习的地方.下文作为学习.工作中用到vector的总结. 1. 赋值运算=的实现原理 在使用赋值操作时,如果不清楚内部是怎么实现,那么用起来会畏手畏脚. ...

  3. 为什么有这么多 Python版本

    Python是出类拔萃的 然而,这是一句非常模棱两可的话.这里的"Python"到底指的是什么? 是Python的抽象接口吗?是Python的通用实现CPython吗(不要把CPy ...

  4. vuex - 辅助函数学习

    官网文档: https://vuex.vuejs.org/zh-cn/api.html  最底部 mapState 此函数返回一个对象,生成计算属性 - 当一个组件需要获取多个状态时候,将这些状态都声 ...

  5. Linux "bring up eth0 failed, eth0 seems not be presernt" 问题解决方案

    =========1.问题========== 重启网卡的时候出现"bring up eth0 failed, eth0 seems not be presernt", 提示找不到 ...

  6. 一键用VS编译脚本

    set MSBUILD_PATH="C:\Program Files (x86)\MSBuild\12.0\Bin\MsBuild.exe" set ZIP_TOOL=" ...

  7. Stooge 排序

    Stooge排序是一种低效的递归排序算法,甚至慢于冒泡排序.在<算法导论>第二版第7章(快速排序)的思考题中被提到,是由Howard.Fine等教授提出的所谓“漂亮的”排序算法. 实现 如 ...

  8. Excel 2007表格内输入http取消自动加上超链接的功能

    经常使用Excel表格工作的也许会发现,当我们在表格内输入http://XXXX时,默认情况下都会自动加上超链接,如下: 当我们点击域名准备编辑修改时,往往都会调用浏览器转到该域名之下,达不到编辑修改 ...

  9. Packetbeat协议扩展开发教程(1)

    Packetbeat ( https://www.elastic.co/products/beats/packetbeat )是一个开源的网络抓包与分析框架,内置了很多常见的协议解析,如HTPP.My ...

  10. 【CF887E】Little Brother 二分+几何

    [CF887E]Little Brother 题意:给你n个圆和一条线段,保证圆和圆.圆和线段所在直线不相交,不相切,不包含.求一个过线段两端点的圆,满足不和任何圆相交(可以相切.包含).问圆的最小半 ...