完全二叉树

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

不曾想,完全二叉树的规律早就知道啊。根结点为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. Storm实时计算框架的编程模式

    storm分布式流式计算框架. nimbus:主进程服务(职责就是任务的分配的,程序的分发) supervisor:工作进程服务(职责就是启动线程池,接受任务,运行任务,报告任务的运行状态) 注意容错 ...

  2. SQL Server 2012 Enterprise Core Edition和SQL Server 2012 Enterprise Edition的区别

    core没有图形界面,只有power shell界面,给没有图形界面的windows用的.

  3. python3 pickle, json

    pickle 有dump ,dumps ,load,loads等方法.区别在于dumps不会写入到文件. import pickle string = ['a', 2341, 'adsf'] p_st ...

  4. '@P0' 附近有语法错误

    问题出在ibatis中的某个orm配置文件,查看你的某些sql语句,尤其是用到#和$等进行赋值的,区分开到底是用#还是$ eg: select top $pagefrom$ id from tb_bo ...

  5. Android开发-Hello World+phonegap(Cordova)

    想着把IOS的程序在Android上实现一下,尝试Android开发,结果发现Android的开发环境也不好弄.接下来记录下整个过程,耳熟能详的操作就不再赘述,重点记录个人特别的经历: 一.安装jav ...

  6. Bootstrap 3 How-To #1 下载与配置

    Bootstrap 3 发布了,通过简单的几步,我们就可以使用 Bootstrap 的样式表,图标,以及 javascript 来配置一个简单的站点. 准备 Bootstrap 不仅仅是一个代码集,还 ...

  7. windows Android开发环境快速搭建和部署

    windows安装Android的开发环境相对来说比较简单,本文写给第一次想在自己Windows上建立Android开发环境的朋友们,为了确保大家能顺利完成开发环境的搭建,文章写的尽量详细,希望对初级 ...

  8. memcached搭建缓存系统

    Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,提升性能. 二.适用场合 1.分布式应用.由于memca ...

  9. MSP430F149学习之路——比较器Comparaor_A

    代码一: #include <msp430x14x.h> ; void int_clk() { BCSCTL1 &= ~XT2OFF; BCSCTL2 |= SELM_2 + SE ...

  10. [Hibernate 2]session的三种状态

    一.Session的特点和获取 特点: Session不是线程安全的,它代表与数据库之间的一次操作,它的概念介于Connection和Transaction之间.  Session也称为持久化管理器, ...