04-树6 Complete Binary Search Tree
完全二叉树
刚开始只发现了中序遍历是从小到大顺序的。一直在找完全二叉树的层结点间规律。。。放弃了
不曾想,完全二叉树的规律早就知道啊。根结点为i,其左孩子结点2*i, 右孩子结点2*i+1。
结合此两者即可解决问题!
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 NN 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 <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; #define MaxSize 1005
int sortNum[MaxSize] = {};
int CBTreeNode[MaxSize] = {};
int countNum = ;
void CreatCBTree(int root,int N)
{
if(root > N)
return;
int left = root * ;
int right = root * + ;
CreatCBTree(left,N); //中序遍历LGR从小到大 小的先
CBTreeNode[root] = sortNum[countNum++];
CreatCBTree(right,N);
} int main()
{
int N;
scanf("%d",&N);
for(int i = ; i < N; i++)
scanf("%d",&sortNum[i]); sort(sortNum,sortNum + N);//按从小到大排序
CreatCBTree(,N);
for(int i = ; i <= N; i++) {
if(i != N)
printf("%d ",CBTreeNode[i]);
else
printf("%d",CBTreeNode[i]);
}
return ;
}
04-树6 Complete Binary Search Tree的更多相关文章
- 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 ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 04-树5 Complete Binary Search Tree
这题也是第二次做,本想第一次做时参考的算法会和老师讲的一样,不想老师讲的算法用在这题感觉还不如思雪园友的算法(http://www.cnblogs.com/sixue/archive/2015/04. ...
- A1064. Complete Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 04-树6 Complete Binary Search Tree(30 分)
title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...
- 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 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- PAT_A1064#Complete Binary Search Tree
Source: PAT A1064 Complete Binary Search Tree (30 分) Description: A Binary Search Tree (BST) is recu ...
- PAT甲级——A1064 Complete Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- 《Code Complete》ch.14 组织直线型的代码
WHAT? 最简单的控制流:即按照先后顺序放置语句与语句块 WHY? 尽管组织直线型的代码是一个简单的任务,但代码结构上的一些微妙之处还是会对代码质量.正确性.可读性和可维护性带来影响 HOW? 必须 ...
- jdk分析之String
public class StringDemo01 { public static void main(String[] args) { String s1 = new String(" ...
- 初探接口测试框架--python系列3
点击标题下「微信」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是我们期 ...
- 解决 Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. 的问题
在web 网站开发中,经常需要连接数据库,有时候会出现这样的数据连接异常消息: 主要原因是 应用程序与数据库的连接超出了数据库连接的默认时长,在这种情况下,我们可以把数据库连接的时长延长一些,因为 C ...
- HTTP 错误 500(Internal Server Error)
今天在用ajax请求页面的时候出现了这么一个错误:HTTP 错误 500(Internal Server Error) 由于提示较少,过了好一阵子才找到答案:ajax请求中调用了一个不存在的函数⊙﹏⊙ ...
- 华为OJ平台——24点游戏
题目描述: 给出4个1-10的数字,通过加减乘除,得到数字为24就算胜利 输入: 4个1-10的数字.[数字允许重复,测试用例保证无异常数字]输出: true or false 思路:
- 在AX4.0中使用C#脚本的实现
1,虽然ms收购了ax,但是在ax低版本(ver<=4.0)中,还没有办法直接使用ms现在主推的.net技术. 通常的做法是现在AX中天津.net的引用,然后才能在代码中使用.net的一些对象以 ...
- leetcode 14
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- Android IOS WebRTC 音视频开发总结(六三)-- 2016国内IM云服务行业分析
本文主要国内IM云服务行业分析,文章最早发表在我们的微信公众号上,详见这里,欢迎关注微信公众号blackerteam,更多详见www.blackerteam.com 谈到IM我们最先想到的是qq和微信 ...
- 【改进版】C++小程序中一个cout输出语句背后的堆栈知识
最开始写这篇文章的时候,凭着自己对汇编的一点理解就堆出了这些内容,经 egmkang的指点,才发觉自己是井底之蛙,花了半天的功夫,去学习顺序点等内容.针对上次写的程序,我决定添一些内容,把程序2后面的 ...