Source:

PAT A1064 Complete Binary Search Tree (30 分)

Description:

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

Keys:

Attention:

  • 很好的一道题,全面考察了完全二叉树和二叉查找树的性质

Code:

 /*
Data: 2019-06-26 16:48:26
Problem: PAT_A1064#Complete Binary Search Tree
AC: 13:22 题目大意:
BST定义:lchild < root <= rchild
给定一个序列,建立CBT和BST,打印层次遍历 基本思路:
一维数组作为CBT的存储结构,结点序号从1~N;
中序遍历CBT,结果递增有序,因此遍历的同时依次赋值排序好的键值即可;
CBT树的遍历:左子树=i*2,右子树=i*2+1,空树i>n
CBT的层次遍历:打印cbt[1]~cbt[n]即可
*/
#include<cstdio>
#include<queue>
using namespace std;
const int M=1e3+;
int cbt[M],n,x;
priority_queue<int,vector<int>,greater<int> > bst; void InOrder(int root)
{
if(root > n)
return;
InOrder(root*);
cbt[root] = bst.top();
bst.pop();
InOrder(root*+);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d", &n);
for(int i=; i<n; i++)
{
scanf("%d", &x);
bst.push(x);
}
InOrder();
for(int i=; i<=n; i++)
printf("%d%c", cbt[i],i==n?'\n':' '); return ;
}

PAT_A1064#Complete Binary Search Tree的更多相关文章

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

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

  2. 04-树5 Complete Binary Search Tree

    这题也是第二次做,本想第一次做时参考的算法会和老师讲的一样,不想老师讲的算法用在这题感觉还不如思雪园友的算法(http://www.cnblogs.com/sixue/archive/2015/04. ...

  3. 04-树6 Complete Binary Search Tree

    完全二叉树 刚开始只发现了中序遍历是从小到大顺序的.一直在找完全二叉树的层结点间规律...放弃了 不曾想,完全二叉树的规律早就知道啊.根结点为i,其左孩子结点2*i, 右孩子结点2*i+1. 结合此两 ...

  4. Complete Binary Search Tree

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  5. A1064. Complete Binary Search Tree

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  6. 04-树6 Complete Binary Search Tree(30 分)

    title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...

  7. PAT 1064 Complete Binary Search Tree[二叉树][难]

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

  8. PAT 甲级 1064 Complete Binary Search Tree

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

  9. 04-树6 Complete Binary Search Tree (30 分)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

随机推荐

  1. MyEclipse6.0中使用aptana插件,添加jquery提示功能

    MyEclipse6.0中使用aptana插件,添加jquery提示功能 第一:查看当前MyEclipse集成的eclipse的版本,, 查看路径    D:/MyEclipse 6.0/eclips ...

  2. J2EE学习篇之--JDBC详解

    今天我们来说一下关于JDBC的相关知识,关于JDBC我想大家都不陌生了,而且我记得早就开始使用它了,记得那是大二的时候做课程设计,但是那时候是为了完成任务,所以遇到问题就google,那时候也没有时间 ...

  3. flink详细介绍

    Flink是什么 Flink是一个分布式计算引擎 MapReduce Spark Storm 同时支持流计算和批处理 和Spark不同, Flink是使用流的思想做批, Spark是采用做批的思想做流 ...

  4. LINIX上Nginx的从零安装

    源码编译方式: #一般系统中已经装了了make和g++,无须再装 安装make: yum -y install autoconf automake make 安装g++: yum -y install ...

  5. 21、Linux命令对服务器网络进行监控

    带宽在我们性能测试中是非常重要的一个因素,带宽的理论上传/下载速度是可以进行推算的.比如你的带宽是10m,那么上传/下载理论速度是10/8=1.25m/s.举个例子,服务器上一个文件大小1.25M,我 ...

  6. PS--工具类

    1.移动工具 快捷点Vctrl + 点击想要移动的图层.选中后,就可以移动了. 2.选取工具 快捷键M2.1选取后填充颜色:新建图层 选取 右键填充 添加前景色 2.2两块选区选择一个选区后,属性面板 ...

  7. selenium 滑动页面至元素可见

    滚动页面 在自动化操作中,如果web页面过长,而我们需要的元素并不在当前可视页面中,那么selenium就无法对其进行操作:此时,我们就需要像平时操作浏览器一样来滚动页面,使我们需要操作的对象可见! ...

  8. 5-MySQL-Ubuntu-操作数据库的基本操作语句

    注意: (1)每一条sql语句都是以分号(;)结尾! (2)数据库的默认charset不支持中文,所以每次在创建数据库的时候要指定字符集charset=utf8; (一) 查看当前时间: select ...

  9. Windows进程调度相关

    结构体所在环境: Windows XP Version 2600 (Service Pack 3) UP Free x86 compatible EPROCESS: ntdll!_EPROCESS + ...

  10. PHP操作XML方法之SimpleXML

    SimpleXML简介 SimpleXML 扩展提供了一个非常简单和易于使用的工具集,能将XML转换成一个带有一般属性选择器和数组迭代器的对象. 举例XML XML结构部分引用自<<深入理 ...