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 ...
随机推荐
- 大牛公司的github
Google Google https://github.com/google Google Samples https://github.com/googlesamples Google Codel ...
- APICloud框架——融云+UIChatTools实现即时通讯聊天
今天完成了公司app的聊天界面的收发消息功能,结合融云2和UIChatTools模块实现,只是实现了基本功能,好多细节还没有实现,废话不多说,上代码 输入框页面(win) 先引入所需模块 // 融云模 ...
- html常用标签梳理
标签的语法 标签由英文尖括号<和>括起来,如<html>就是一个标签. html中的标签一般都是成对出现的,分开始标签和结束标签.结束标签比开始标签多了一个/. 如: (1)& ...
- NX二次开发-UFUN打开本地文本文档uc4504
NX9+VS2012 #include <uf.h> #include <uf_cfi.h> #include <uf_ui.h> using std::strin ...
- python内置模块-random
print(random.randint(1,10)) 生成随机整数,下限必须小于上限print(random.randrange(1,10)) 生成随机整数,参数为([start],stop,[st ...
- [NOIP]模拟17 题解
A.入阵曲 部分分很肥,正解写得常数稍大就会和暴力一个分,考试的时候写什么自己考虑.(滑稽 部分分的循环边界手抖写错了-25 (原本暴力分中的10分都没了啊啊啊) 没写挂的话应该有75,其实就是二维前 ...
- date和string转换用joda包
import org.joda.time.DateTime;import org.joda.time.format.DateTimeFormat;import org.joda.time.format ...
- winform界面设计
http://www.cnblogs.com/wuhuacong/ 这位大师给了我指导方向 http://officeribbon.codeplex.com 提供了ribbon界面的控件 动态web ...
- leetcode 596 BUG笔记
There is a table courses with columns: student and class Please list out all classes which have more ...
- Keepalived 双主虚拟路由配置实例
Keepalived 双主虚拟路由配置实例 演示前说明: 2台centos7.2 主机:node-00,node-01 VIP1:10.1.38.19预定node-00占有 VIP2:10.1.38. ...