完全二叉树

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

不曾想,完全二叉树的规律早就知道啊。根结点为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. Oracle Dataguard Standby Redo Log的两个实验

    在Data Guard环境中,Standby Redo Log是一个比较特殊的日志类型.从最新的DG安装指导中,都推荐在Primary和Standby端,都配置Standby Redo Log. 简单 ...

  2. 十步让 WebForm项目 变为 Mvc项目

    1.创建一个项目名为 App_Asp 的 Asp.NET 空 Web 应用程序2.添加全局应用程序类 Global.asax3.富文本打开 Global,修改 Inherits 为 App_Asp_G ...

  3. SVN小小用法(一)svn服务器搭建

    最近由于公司项目用SVN作为版本控制工具,本着学一点是一点的原则,今天小配了下svn,给大家介绍一下 软件:TortoiseSVN-1.8.3.24901-win32-svn-1.8.4.msi(本人 ...

  4. Sqoop2环境搭建

    正在准备做Spark SQL external data source与关系型数据库交互的部分,参考下Sqoop2是如何操作关系型数据库的. 下载地址:http://archive.cloudera. ...

  5. Kendo UI - Class 基类定义

    在 kendo 中,使用原型继承机制,Class 是 Kendo 中的基类,定义了函数 extend 用来派生其它类. function Class() {} Class.extend = funct ...

  6. struts2下实现的json传递list,object。

    必须的jar: java bean: package upload.progress.action; public class music { private String name; private ...

  7. DP走方格型

    Hrbust 1812  http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1812 有 ...

  8. 【Unity Shaders】学习笔记——SurfaceShader(二)两个结构体和CG类型

    [Unity Shaders]学习笔记——SurfaceShader(二)两个结构体和CG类型 转载请注明出处:http://www.cnblogs.com/-867259206/p/5596698. ...

  9. [翻译]你不会想知道的kobject,kset,和ktypes

    ---------------------------------------------------------------------------------------------------- ...

  10. 如何在使用 RemoteWebDriver 打开网页的同时获取 Http 状态码

    最近一直在用Selenium这个开源项目写一些web 自动化的小玩意.本来一直运行的挺好,直到有一天突然发现资源抓取失败了,翻看日志才发现,原来本该正常打开的页面返回了504错误所以自然失败了.如何避 ...