pat1064. Complete Binary Search Tree (30)
1064. 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 (<=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)的更多相关文章
- 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 ...
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 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)【二叉树】——PAT (Advanced Level) Practise
题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...
- 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 ...
- PAT1064. Complete Binary Search Tree
1064. Complete Binary Search Tree 题目大意 给定一个序列, 求其 生成Complete BST 的层序遍历. 思路 最开始把这个题想复杂了, 还想着建立结构体, 其实 ...
- 04-树6 Complete Binary Search Tree (30 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- logback 相关
%logger{36} 表示logger名字最长36个字符,否则按照句点分割. %X{key} to get the value that are stored in the MDC map ${lo ...
- altium designer 中的top/bottom solder和top/bottom paste mask
转载请注明出处:http://blog.csdn.net/qq_26093511/article/details/51751936 1.top solder为助焊层,说白一点就是说,有这个层的地方就没 ...
- VisualGDB系列7:使用VS创建Linux静态库和动态库
根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何在VS中创建静态库和动态库, ...
- makefile基本语法
多文件编程的小例子功能:在main.c里面调用其他两个源文件里面的函数,然后输出字符串. 1.main.c #include"mytool1.h"#include" ...
- 杭电acm 1098题
Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no choice bu ...
- 10.Ubuntu16搭建蜜罐Cowrie
一直都想尝试搭建一个蜜罐,因为蜜罐是一款可以对ssh,telnet,http等等协议攻击进行记录,对于输入命令和上传文件均有记录的一款软件. 记录可以设置在日志文件中或者通过配置数据库,导入数据库中方 ...
- 没有定义json_encode()函数。
php5是没有json扩展的,需要自行下载. 命令php -m 可以查看安装了哪些扩展. 1.修改php.ini 在php.ini 中加入 extension=json.so:sudo vi /etc ...
- Dapper 存储过程、事务等
接上一篇<Dapper 增删改查> 0.存储过程 create proc p_login ), ), ) output as begin if(exists(select * from U ...
- VS Code(待补充)
使用! 然后Tab 快速生成html文档结构 快速生成一个类 .类名 会有提示 .container.box .col-6*2 VisualStudio Code怎么同时编辑多处?
- 洛谷P1441 砝码称重
P1441 砝码称重 题目描述 现有n个砝码,重量分别为a1,a2,a3,……,an,在去掉m个砝码后,问最多能称量出多少不同的重量(不包括0). 输入输出格式 输入格式: 输入文件weight.in ...