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. 【Dart学习】-- Dart之操作符

    一,概述 dart定义了下表所示的运算符.你可以重写许多这些运算符. 描述 运算符 一元后缀 expr++ expr-- () [] . ?. 一元前缀 -expr !expr ~expr ++exp ...

  2. 天照(amaterasu)

    天照(amaterasu) 有些时候,出题人真的不想写背景. 总而言之,天照现在有一个长度为 $ N $ 序列,她有 $ M $ 次询问,对于第 $ i $ 次询问 $ l_i,r_i,x_i $ 你 ...

  3. 网页压缩--gzip和deflate的区别

    我们在配置网站GZip压缩的时候,会发现有两个模块可以设置的,一个是GZip模块的参数配置,另一个是Deflate模块的参数配置,他们的设置方法是一样的.刚开始时我不太明白,这两地方有什么不同?网站开 ...

  4. LaTex 插入图像,以及应用表格

    插入图像 参考:http://www.ctex.org/documents/latex/graphics/ 1: \includegraphics[width=20mm]{head.png} 应用表格 ...

  5. 简单介绍Collection框架的结构

    Collection:List列表,Set集 Map:Hashtable,HashMap,TreeMap Collection  是单列集合 List   元素是有序的.可重复 有序的 collect ...

  6. hbase之setCaching 和 setBatch 和setMaxResultSize

    scan的setBatch()用法 val conf = HBaseConfiguration.create() val table: Table = ConnectionFactory.create ...

  7. vant实现三级联动

    首先要在vant 框架里边   复制一下   省市区的 地址数据在这里下载eare.js 格式 : var address = { province_list: { 110000: '北京市', }, ...

  8. Kibana < 6.6.1 代码执行漏洞复现CVE-2019-7609

    登陆kibana poc .es(*).props(label.__proto__.env.AAAA='require("child_process").exec("ba ...

  9. 拾遗:Git 常用操作回顾

    温故而知新,可以为师矣. Git 布局 工作区---->暂存区---->本地仓库---->远程仓库 Create Repository git init PATH git add P ...

  10. adapter设计模式

    适配器设计模式 将一个类的接口转换成客户希望的另外一个接口.Adapter 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作 例如:现在有一个220V的插口,而手机不能直接接上去,因为锂电 ...