1064. Complete Binary Search Tree (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

提交代码

 #include<cstdio>
#include<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<cmath>
using namespace std;
int line[],tree[];
void Build(int *line,int *tree,int n,int cur){
if(!n){
return;
}
int h=log(n)/log();
int x=n-(pow(,h)-); //cout<<"x: "<<x<<endl; if(x>pow(,h-)){
x=pow(,h-);
} //cout<<"x: "<<x<<endl; int l=pow(,h-)+x-; //cout<<"l: "<<l<<endl; tree[cur]=line[l];
Build(line,tree,l,cur*+);
Build(line+l+,tree,n-l-,cur*+);
}
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n,i;
scanf("%d",&n);
for(i=;i<n;i++){
scanf("%d",&line[i]);
}
sort(line,line+n);
Build(line,tree,n,);
printf("%d",tree[]);
for(i=;i<n;i++){
printf(" %d",tree[i]);
}
printf("\n");
return ;
}

pat1064. Complete Binary Search Tree (30)的更多相关文章

  1. PAT题库-1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  2. pat04-树8. Complete Binary Search Tree (30)

    04-树8. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHE ...

  3. pat 甲级 1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  4. 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 ...

  5. 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 Tr ...

  6. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

    1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a bin ...

  7. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  8. PAT1064. Complete Binary Search Tree

    1064. Complete Binary Search Tree 题目大意 给定一个序列, 求其 生成Complete BST 的层序遍历. 思路 最开始把这个题想复杂了, 还想着建立结构体, 其实 ...

  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. Myeclipse如何使用Maven添加jar包

    很多新手都不知道如何在maven项目里添加jar包. 以前我还没接触maven的时候下载过一个demo,是maven项目. 我居然是照着他的pom.xml文件一个一个的写!!! 很多人认为理所当然的东 ...

  2. bzoj4403

    组合数学 我好菜啊 想到dp去了... 事实上对于固定长度的数列,我们只用考虑选了哪些数就行了,所以这个就是$C(n+m-1,m-1)$ 也就是$n$个数,划分成$m$段且允许空的方案数 然后变成$\ ...

  3. ruby on rails 环境搭建步骤

    1.安装ruby ruby的下载页面一个版本有3样要下载的,帮助文件和安装文件.还有一个mingw. 安装时抛出make出错信息就是由于没有安装mingw引起的 到下载页http://rubyforg ...

  4. man syslog | col -b > syslog.txt

    linux man手册导出,解决man乱码 man syslog | col -b > syslog.txt

  5. Android开发者学习必备:10个优质的源码供大家学习

    最近看了一些开发的东西,汇总了一些源码.希望可以给大家有些帮助! 1.Android 源码解析—PagerSlidingTabStrippagerSlidingTabStrip 实现联动效果的原理是, ...

  6. linux命令-vim命令模式

    编辑模式下 按 “esc” 进去命令模式 :wq 保存并退出 :w  保存 已写入 :q!不保存强制退出 :wq! 强制保存退出 :set nu 显示行号 :set nonu 去掉行号 :nohl 取 ...

  7. Java探索之旅(2)——GUI输入输出与代码的规范性

    1.知识点概叙 ① 定名常量:关键字final,类似C++ const定义,一般用大写:final double PI=3.1415926 ② 5/2=2:5.0/2=2.5://通常意义的除法,至少 ...

  8. 大内存电脑在vbox安装linux报错

    问题描述: 1.机器:Linux主机,特别是主机为大内存,比如: 4G内存的使用pae内核的Ubuntu系统的thinkpad电脑. 2.情况:使用VirtualBox安装Linux系统时,比如:通过 ...

  9. Spring入门第四课

    注入参数详解:null值和级联属性 可以使用专用的<null/>元素标签为Bean的字符串或其他对象类型的属性注入null值. 和Struts,Hiberante等框架一样,Spring支 ...

  10. python远程登录Paramiko模块的安装

    最近做云平台的项目,需要使用python来管理所有的主机,我选择了paramiko.它跨平台的,linux和window都可以使用,pssh只支持linux. 1:安装gcc和python-devel ...