1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
题目信息
1064. Complete Binary Search Tree (30)
时间限制100 ms
内存限制65536 kB
代码长度限制16000 B
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 (<=1000). 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
解题思路
模拟建树推出层序
AC代码
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int n, a[1005];
vector<int> level[22];
void step(int loc, int len, int lv){
if (len <= 0) return;
int t = 1;
while (t*2 <= len) t *= 2;
--t;
int cd = ((len - (len - t)) + 1) / 2 + len - t;
if (cd <= t + 1){
level[lv].push_back(a[loc + cd - 1]);
step(loc, cd - 1, lv + 1);
step(loc + cd, len - cd, lv + 1);
}else{
level[lv].push_back(a[loc + t]);
step(loc, t, lv + 1);
step(loc + t + 1, len - t - 1, lv + 1);
}
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; ++i){
scanf("%d", a+i);
}
sort(a, a + n);
step(0, n, 0);
printf("%d", level[0][0]);
for (int i = 1; i <= 13; ++i){
for (int j = 0; j < level[i].size(); ++j){
printf(" %d", level[i][j]);
}
}
printf("\n");
}
个人游戏推广: apkName=com.xianyun.yf" target="_blank" align="left"> apkName=com.xianyun.yf" target="_blank" align="left">《10云方》与方块来次消除大战!
1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise的更多相关文章
- pat 甲级 1064. 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 分) 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 ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- PAT Advanced 1064 Complete Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- 1064 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)-中序和层次遍历,水
由于是满二叉树,用数组既可以表示父节点是i,则左孩子是2*i,右孩子是2*i+1另外根据二分搜索树的性质,中序遍历恰好是从小到大排序因此先中序遍历填充节点对应的值,然后再层次遍历输出即可. 又是一道遍 ...
- 【PAT甲级】1064 Complete Binary Search Tree (30 分)
题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...
- PAT (Advanced Level) 1064. Complete Binary Search Tree (30)
因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include& ...
随机推荐
- Angular——内置服务
$location <!DOCTYPE html> <html lang="en" ng-app="App"> <head> ...
- 最容易理解的HMM文章
wiki上一个比较好的HMM例子 分类 隐马尔科夫模型 HMM(隐马尔科夫模型)是自然语言处理中的一个基本模型,用途比较广泛,如汉语分词.词性标注及语音识别等,在NLP中占有很重要的地位.网上关于HM ...
- 创建一个TCP服务器端通信程序的步骤
创建一个TCP服务器端通信程序的步骤: 1). 创建一个ServerSocket 2). 从ServerSocket接受客户连接请求 3). 创建一个服务线程处理新的连接 4). 在服务线程中,从so ...
- Vue指令6:v-show
根据表达式的真假值来渲染元素 用法大致一样: <h1 v-show="ok">Hello!</h1> 不同的是带有 v-show 的元素始终会被渲染并保留在 ...
- CAD控件:COM接口实现自定义实体
1. 实现步骤: 3 1. 实现步骤: 参考例子 :Src\MxDraw5.2\samples\ie\iedemoTest.htm 1) 增加自定义实体对象 调用DrawCustomEntity函数, ...
- 怎么让Eclipse对html和js代码自动提示
使用eclipse自带的插件,无需另外安装插件,具体步骤如下1.打开eclipse→Windows→Preferences→Java→Editor→Content Assist修改Auto Activ ...
- Microsoft SQL Server Transact-SQL
Microsoft SQL Server Transact-SQL 1.SQL 1.1数据定义语言(DDL) create 创建数据库或数据库对象:alter 修改数据库或数据库对象:drop 删除数 ...
- POJ2454——Jersey Politics
POJ2454——Jersey Politics 题目大意: 在泽西奶牛和荷斯坦奶牛的最新普查中,威斯康星奶牛在谷仓中获得了三个档位. 泽西奶牛目前控制着国家重新分配委员会. 他们想将国家分为三个相当 ...
- NOIP 2018 真・退役记
目录 NOIp 2018 真・退役记 7.01 7.05 \(summary\) 7.12 7.18 7.26 - 7.27 8.2 8.3 8.3 8.7 8.9 8.20 8.24 8.27 8. ...
- 一步一步实现基于GPU的pathtracer(三):path tracing 简述
全局光照这个名词在计算机图形学里已经不算一个新名词了,现在一提到拟真度,很多人基本上都会去想到全局光照,这个名词上世纪七八十年代就有了,好像是由一个叫Jim Kajiya的大神在他那篇已经被引用了不知 ...