04-树6 Complete Binary Search Tree (30 分)
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
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ;
int CBT[maxn],index = ,num[maxn];
int n; void inOrder(int root){
if(root > n) return;
inOrder(root*);
CBT[root] = num[index++];
inOrder(root*+);
} int main(){
scanf("%d",&n);
for(int i = ; i < n; i++){
scanf("%d",&num[i]);
}
sort(num,num+n);
inOrder();
for(int i = ; i <= n; i++){
printf("%d",CBT[i]);
if(i < n) printf(" ");
}
return ;
}
04-树6 Complete Binary Search Tree (30 分)的更多相关文章
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PTA 04-树6 Complete Binary Search Tree (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree (30分) A ...
- 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 04-树6 Complete Binary Search Tree (30 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 【PAT甲级】1064 Complete Binary Search Tree (30 分)
题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- pat04-树8. Complete Binary Search Tree (30)
04-树8. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHE ...
- pat1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
随机推荐
- [GO]工程管理
同级目录: 1.分文件编程,多个源文件,必须放在src目录 2.设置GOPATH目录,目录地址不包括src后缀 3.同一个目录,包名必须一样 4.go env查看go相关的环境路径 5.同一个目录,调 ...
- oracle数据库之数据插入、修改和删除
作为一合格的测试人员对数据库的单表查询.多表查询.分组查询.子查询等等这些基本查询方法还是要会的.不然到企业中,容易被一些人鄙视,或者说如果数据库学不好,表查不明白,那么对自己能力来说也是一种侮辱,因 ...
- 升级Ubuntu 12.04下的gcc到4.7
我们知道C++11标准开始支持类内初始化(in-class initializer),Qt creator编译出现error,不支持这个特性,原因在于,Ubuntu12.04默认的是使用gcc4.6, ...
- 《Wonderland: A Novel Abstraction-Based Out-Of-Core Graph Processing System》章明星
在2018年3月28日于美国弗吉尼亚州威廉斯堡结束的ACM ASPLOS 2018会议上,计算机系高性能所师生发表了两篇长文.一篇是我系博士生章明星为第一作者,导师武永卫为通讯作者的“Wonderla ...
- android开关控件Switch和ToggleButton
序:今天项目中用到了开关按钮控件,查阅了一些资料特地写了这篇博客记录下. 1.Switch <Switch android:id="@+id/bt" android:layo ...
- Java java.lang.Thread#join()方法分析
结论:A 线程调用 B 线程对象的 join 方法,则 A 线程会被阻塞,直到 B 线程 挂掉 (Java Doc 原话: Watis for this thread to die). 一.分析 查看 ...
- 二、搭建SpringBoot项目
与其说是搭建,还不如说去下载...(注意,在此之前要确保你的3000块钱的笔记本上安装了JDK8+已经最新的相对较新的maven:apache-maven-3.6.0,至于JDK以及maven的相关安 ...
- Selenium API(一)
1.设置浏览器大小: # -*- coding:utf- -*- from selenium import webdriver import time driver = webdriver.Firef ...
- Java bean与Spring、Spring MVC关系
Java Bean Java语言欠缺属性.事件.多重继承功能.所以,如果要在Java程序中实现一些面向对象编程的常见需求,只能手写大量胶水代码.Java Bean正是编写这套胶水代码的惯用模式或约定. ...
- The server of Apache (二)——apache服务客户端验证
一.确定网站名称.IP地址 地址为: 192.168.1.1 域名为: www.benet.com 二.配置可用的DNS域名服务或者修改本地hosts记录 ~] # vim /etc/hosts ...