PAT1064: Compelte Binary Search Tree
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 思路
1.二叉搜索树的中序遍历是一个递增的序列,所以先将输入后的节点数组升序排序。
2.完全二叉树以数组描述时,索引为i的节点的左右孩子的索引分别为2 * i和2 * i + 1。
3.将排好序的节点以中序遍历的形式来构造完全二叉搜索树。
4.遍历构造好的数组即为这棵树的层次遍历
代码
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std; const int MAX = ;
vector<int> nodes(MAX);
vector<int> tree(MAX);
int N,index; //number of nodes && index of Nodes void buildtree(int root)
{
if(root > N)
return;
int left = root*,right = root* + ;
buildtree(left);
tree[root] = nodes[index++];
buildtree(right); } int main()
{
while(cin >> N)
{
//input
for(int i = ; i <= N;i++)
cin >> nodes[i];
sort(nodes.begin() + ,nodes.begin() + + N);
//build tree
index = ;
buildtree(); //print tree
cout << tree[];
for(int i = ;i <= N;i++)
cout <<" " << tree[i];
cout << endl;
}
}
PAT1064: Compelte Binary Search Tree的更多相关文章
- PAT1064. Complete Binary Search Tree
1064. Complete Binary Search Tree 题目大意 给定一个序列, 求其 生成Complete BST 的层序遍历. 思路 最开始把这个题想复杂了, 还想着建立结构体, 其实 ...
- 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(完全二叉树)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT-1064(Complete Binary Search Tree)JAVA实现
Complete Binary Search Tree PAT-1064 本次因为涉及到完全二叉排序树,所以可以使用数组的形式来存储二叉排序树 对输入序列排序后,得到的是中序遍历二叉排序树的序列.对这 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
随机推荐
- [FreeRadius2]遇到问题记录
在学习FreeRadius2中遇到的问题,和解决. 使用的是2.2 版本,测试的系统是Centos6.7 radtest 没有响应 radiusd 启动正常,测试如下命令不好使 [root@orang ...
- (C++)string类杂记
本文特记录C++中string类(注意string是一个类)的一些值得注意的地方. string类的实例是以‘\0'结束的吗? 这个问题有时还真容易混淆,因为我们可能会将 C++ 语言中的string ...
- BDA大数据处理流程
可以看出,数据处理用云,可以高效完成.而分析部分应该利用传统的bi工具.
- 【freeradius3】安装和拓展需求
这次主要来看看 freeradius的安装,以及Python拓展的例子,还有计费字段根据厂家进行拓展. 3.0版本的安装 参考文章 yum install libtalloc-devel wget - ...
- 【Android 应用开发】Android 开发错误集锦
1. eclipse的Device中不显示手机 在eclipse中连接不上手机,出现adb server didn't ACK fail to start daemon 错误. 出现这种原因是因为a ...
- disable table 失败的处理
相信每一个维护hbase集群的运维人员一定碰到过disable失败,陷入无穷的"Region has been PENDING_CLOSE for too long..."状态,此 ...
- Http的会话跟踪和跨站攻击(xss)
会话跟踪 什么是会话? 客户端打开与服务器的连接发出请求到服务器响应客户端请求的全过程称之为会话. 什么是会话跟踪? 会话跟踪指的是对同一个用户对服务器的连续的请求和接受响应的监视. 为什么需要会话跟 ...
- obj-c编程10:Foundation库中类的使用(1)[数字,字符串]
我们知道在mac或iphone上编程最终逃不开os x平台,你无法在windows或linux上开发纯正的apple程序.(so不要舍不得银子买mac啦)虽说linux和windows上有移植的obj ...
- MySQL 和 JDBC(Java数据库连接)
1.MySQL 1.1 MySQL简介 a)MySQL是一个开源免费的关系型数据库管理系统. b)默认用户:root c)默认端口号: 2.MySQL常用命令 2.1连接MySQL mysql ...
- Day7 面向对象和类的介绍
面向对象讲解: ''' 面向过程: 核心是过程二字,过程指的是问题的解决步骤,基于过程去设计程序,就好比在设计一条流水线,是一种机械式的思维方式. 优点:复杂的问题流程化,进而简单化 缺点:可扩展性差 ...