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 ...
随机推荐
- Java-transient总结
纸上得来终觉浅,绝知此事要躬行 --陆游 问渠那得清如许,为有源头活水来 --朱熹 transient有"临时的","短暂的"含义,我们了解过Seri ...
- Boosting 和梯度Boosting
Boosting方法: Boosting这其实思想相当的简单,大概是,对一份数据,建立M个模型(比如分类),一般这种模型比较简单,称为弱分类器(weak learner)每次分类都将上一次分错的数据权 ...
- inittab文件剖析[CentOS 5.X](第二版)
inittab文件剖析[CentOS 5.X] grep -v "^#" /etc/inittab | more #过滤掉以#开头的行 inittab文件格式 id:runleve ...
- Erlang Rebar 使用指南之二:制作发布版本
Erlang Rebar 使用指南之二:制作发布版本 全文目录: https://github.com/rebar/rebar/wiki 本章位置: https://github.com/rebar/ ...
- Gradle 1.12用户指南翻译——第三十七章. OSGi 插件
本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- LeetCode - 验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索树. ...
- Jedis对Redis的常用命令操作
本篇主要总结一些Jedis对Redis的常用命令操作: 1.对key操作命令 2.对String操作命令 3.对List操作命令 4.对Set操作命令 5.对Hash操作命令 6.排序操作指令 一.项 ...
- 实现Redhat Linux 6和Windows通过Windows Server AD统一认证并共享访问Oracle ZS存储系统
Windows Server 2012 AD设置 1. 建立新的组织单位OU 为用户提前建立好OU,是为了AD用户管理简单清晰. 2. 建立新的用户和用户组 建立新的用户的时候,要同时将用户归属到 ...
- easyui 在编辑状态下,动态修改其他列值。
首先是自定义了一个方法uodateColumn更新列值 /** *自定义的修改列值方法 */ $.extend($.fn.datagrid.methods, { updateColumn: funct ...
- 排序算法的C语言实现(下 线性时间排序:计数排序与基数排序)
计数排序 计数排序是一种高效的线性排序. 它通过计算一个集合中元素出现的次数来确定集合如何排序.不同于插入排序.快速排序等基于元素比较的排序,计数排序是不需要进行元素比较的,而且它的运行效率要比效率为 ...