完全二叉树

刚开始只发现了中序遍历是从小到大顺序的。一直在找完全二叉树的层结点间规律。。。放弃了

不曾想,完全二叉树的规律早就知道啊。根结点为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的更多相关文章

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

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

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

  3. 04-树5 Complete Binary Search Tree

    这题也是第二次做,本想第一次做时参考的算法会和老师讲的一样,不想老师讲的算法用在这题感觉还不如思雪园友的算法(http://www.cnblogs.com/sixue/archive/2015/04. ...

  4. A1064. Complete Binary Search Tree

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  5. 04-树6 Complete Binary Search Tree(30 分)

    title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...

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

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

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

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

  8. PAT_A1064#Complete Binary Search Tree

    Source: PAT A1064 Complete Binary Search Tree (30 分) Description: A Binary Search Tree (BST) is recu ...

  9. PAT甲级——A1064 Complete Binary Search Tree

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  10. 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

随机推荐

  1. OpenStack Nova 制作 Windows 镜像

    OpenStack Nova 制作 Windows 镜像   windows虚拟机ubuntuimage防火墙云计算 本贴转自http://www.vpsee.com 上次 VPSee 给 OpenS ...

  2. 应用层(一)HTTP服务访问基本流程和HTTP报文详解

    HTTP属于TCP/IP模型中一个面向文本的应用层协议,所使用的服务器端口号的TCP中的80端口,通信双方在这个基础上进行通信. 每个服务器都有一个应用进程,时刻监听着80端口的用户访问请求.当有用户 ...

  3. Flex开发自定义控件

    前期准备: 点击File菜单 -> New -> MXML Component,然后弹出一个对话框. 在对话框中输入组件名,选择此组件继承的类型,如:Canvas,DataGrid,Com ...

  4. 【测试】自行建表并演示append+nologging,并描述数据写入后产生的效果

    ①创建表: SQL> create table t4 as select * from all_objects; Table created. ②设置t4处于nologging: SQL> ...

  5. 查询数据库中表或视图或存储过程的数量 sql 语句

    如果一个数据库中表的数量较多的话,那么在统计数据库中表的数量或视图或存储过程的数量时,如果还有一个一个去数,那就太麻烦了,其实可以通过 sql 语句来查询的,sql 语句的查询方法如下: sql se ...

  6. mysql中case用法

    如上所述,使用case的子句将作为一个字段,方便起见,可以用别名表示.其中,when是case的条件,值为then的表达式值.   参考: http://www.owe-love.com/myspac ...

  7. 图解CISCO 3550忘记密码解决方法

    图解CISCO3550忘记密码解决方法 Cisco网络设备密码忘记怎么初始出厂默认值?这时网友常常会提出的问题,怎么解决,有人说啦,去百度去谷歌一下不就行啦,然而这对初学者任是个挑战,虽然步骤只有简单 ...

  8. vyos (一) 基础配置

    http://www.lowefamily.com.au/2015/11/29/using-a-vyos-router-with-hyper-v/1/ http://thomasvochten.com ...

  9. c++ 实现百度自动搜索

    void CAttendanceRobotDlg::DocumentCompleteExplorer4(LPDISPATCH pDisp, VARIANT* URL) { // TODO: Add y ...

  10. Windows 2003 + IIS6.0 相关 401.1 或 401.2 等问题解决

    我自己处理的过程: 1. 问题场景: 1.1 我本身有一个虚拟机为 win2003 + iis6.0 的环境 c:\ 为NTFS 格式 D:\为Fat32 格式 1.2 我需要部署一个 asp.net ...