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. HTML5自定义属性的设置与获取

    <div id="box" aaa="bbb" data-info="hello"></div> <body& ...

  2. yum设置代理

    echo "proxy=http://[proxy_url]:8080" >> /etc/yum.conf

  3. 【Stanford Machine Learning Open Course】学习笔记目录

    这里是斯坦福大学机器学习网络课程的学习笔记. 课程地址是:https://class.coursera.org/ml-2012-002/lecture/index 课程资料百度网盘分享链接:https ...

  4. 拾遗:btrfs

    #扫描 btrfs 文件系统btrfs device scan btrfs device scan /dev/sda #创建子卷或快照 btrfs subvolume create /mnt/btrf ...

  5. Jackson环境安装设置

    本地环境设置 由于Jackson是基于Java编程语言的,所以需要设置Java开发环境,这里介绍如何下载安装设置Java.请按照以下步骤来设置环境. Java SE是免费的,点击下载链接:下载Java ...

  6. 6-vim-移动命令-01-方向和行内移动

    移动 命令模式下快速移动光标 编辑操作命令与移动命令结合使用 1.上下左右 命令 功能 手指 h 向左 食指 j 向下 食指 k 向上 中指 l 向右 无名指 2.行内移动 命令 英文 功能 w wo ...

  7. ForEach控制器

    ForEach一般是和用户定义的变量结合使用的 前面前缀一般是和用户定义的变量前缀一致 输出的变量是要使用的变量 这样能保证每次使用的变量不一样 add"_"before numb ...

  8. 关于apache 重定向设定

    本人在研究关于apache重定向的资料,在网上找了很多,但是就本人来说,方便理解的,找到了这么一个,记录了下来,原帖地址:http://www.exehack.net/8.html 关于apache配 ...

  9. 【颓废篇】Py:从零开始的poj自动提交

    之前学习了一些python的爬虫技术... 已经可以通过python来水blog的阅读量了 你知道的太多了, 然而你看我这个blog惨不忍睹的访问量, 有人吗? 有人吗? 今天突然又双叒叕心血来潮想写 ...

  10. 分布式版本控制工具Mercurial------Linux下hg命令的使用

    1.关于hg命令选项: 每一个每一个命令选项都有一个长的名称,如hg log 命令会使用 –rev选项; 大多数选项拥有一个短的名称,如—rev有一个-r的短名: 长名称以两条横线(–)作为起始,短名 ...