PAT_A1064#Complete Binary Search Tree
Source:
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:
- 完全二叉树(Complete Binary Tree)
- 二叉查找树(Binary Search Tree)
- 二叉树的建立
- 二叉树的遍历
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的更多相关文章
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 04-树5 Complete Binary Search Tree
这题也是第二次做,本想第一次做时参考的算法会和老师讲的一样,不想老师讲的算法用在这题感觉还不如思雪园友的算法(http://www.cnblogs.com/sixue/archive/2015/04. ...
- 04-树6 Complete Binary Search Tree
完全二叉树 刚开始只发现了中序遍历是从小到大顺序的.一直在找完全二叉树的层结点间规律...放弃了 不曾想,完全二叉树的规律早就知道啊.根结点为i,其左孩子结点2*i, 右孩子结点2*i+1. 结合此两 ...
- Complete Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- A1064. Complete Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 04-树6 Complete Binary Search Tree(30 分)
title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...
- PAT 1064 Complete Binary Search Tree[二叉树][难]
1064 Complete Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a b ...
- PAT 甲级 1064 Complete Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...
- 04-树6 Complete Binary Search Tree (30 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- 高级运维(三):部署Lnmp环境、构建Lnmp平台、地址重写
一.部署LNMP环境 目标: 安装部署Nginx.MariaDB.PHP环境 1> 安装部署Nginx.MariaDB.PHP.PHP-FPM: 2> 启动Nginx.MariaDB.FP ...
- Sophus安装时Eigen库更新版本的问题
原文链接:https://blog.csdn.net/m0_38055352/article/details/102900516
- 删除maven项目后eclipse无法启动
An internal error occurred during: "reload maven project". java.lang.NullPointerExceptio ...
- 带头结点的循环单链表----------C语言
/***************************************************** Author:Simon_Kly Version:0.1 Date: 20170520 D ...
- PowerDesigner(数据库迁移)
PowerDesigner:导出SQL脚本以及问题解决在PowerDesigner中点击DataBase -----------> Generate Datebase -----(可以点击Pre ...
- 10. Python面向对象
Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.如果接触过java语言同学应该都知道,Java面向对象三大特征是:封装.继承.多态.Pytho ...
- ajaxFileUpload.js插件支持多文件上传的方法
前提条件:ajaxFileUpload.js插件多文件上传步骤:1.修改源码,(源码只支持单个文件的上传):复制代码 代码如下: //修改前代码------- //var oldElement = j ...
- 防止DDOS攻击有效方法:隐藏服务器真实IP
如今,网站服务器的安全受到越来越多的重视,但是难免会遇到黑客使用DDoS攻击网站,为了网站的安全通常都会做好防御,其中防止DDoS攻击有效方法:隐藏服务器真实IP ,该技术能够有效地保护网站的安全. ...
- windows平板软件开机自启动+霸屏的操作方法
转载(忘了地址) 很好很强大.成功亲测 使用你自己的账号(最好是管理员权限的账号)登录Windows,然后添加一个给其他人使用的账户(假设为other),注意一定要为other设置密码. 运行 ...
- C#跨线程访问(二)----thread参数、回调传参数
一.单个参数(封箱也可实现多参数) class B { public static void Main() { Thread t = new Thread(ne ...