1064. Complete Binary Search Tree

题目大意

给定一个序列, 求其 生成Complete BST 的层序遍历.

思路

最开始把这个题想复杂了, 还想着建立结构体, 其实用数组最为方便简单

  1. 中序遍历这棵 Complete BST, 依次填入一个排好序的序列.
  2. 因为是数组存放, 所以直接输出 1 - nNode 就好.

代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#define MAXN 2000
using namespace std;
int nNode;
int curSeq = 0;
vector<int> seq;
int arr[MAXN];
void dfs(int root){
if(root > nNode)
return;
dfs(root << 1);
arr[root] = seq[curSeq++];
dfs(root << 1 | 1);
}
int main(){
scanf("%d", &nNode);
seq.resize(nNode);
for(int i = 0; i < nNode; i++)
scanf("%d", &seq[i]);
sort(seq.begin(), seq.end());
dfs(1); for(int i = 1; i <= nNode; i++){
if(i != 1) printf(" ");
printf("%d", arr[i]);
}
return 0;
}

PAT1064. Complete Binary Search Tree的更多相关文章

  1. pat1064. Complete Binary Search Tree (30)

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

  2. PAT-1064 Complete Binary Search Tree(完全二叉树)

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

  3. PAT-1064(Complete Binary Search Tree)JAVA实现

    Complete Binary Search Tree PAT-1064 本次因为涉及到完全二叉排序树,所以可以使用数组的形式来存储二叉排序树 对输入序列排序后,得到的是中序遍历二叉排序树的序列.对这 ...

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

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

  5. 04-树5 Complete Binary Search Tree

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

  6. 04-树6 Complete Binary Search Tree

    完全二叉树 刚开始只发现了中序遍历是从小到大顺序的.一直在找完全二叉树的层结点间规律...放弃了 不曾想,完全二叉树的规律早就知道啊.根结点为i,其左孩子结点2*i, 右孩子结点2*i+1. 结合此两 ...

  7. PAT1064: Compelte Binary Search Tree

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

  8. Complete Binary Search Tree

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

  9. A1064. Complete Binary Search Tree

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

随机推荐

  1. dojo入门

    1.引入dojo.js dojo的发行包里有4个子目录,要引入的文件是名叫"dojo"的子目录里的dojo.js. 假设你是这样的目录结构: project | +--dojo-l ...

  2. 简单springboot及springboot cloud环境搭建

    springboot使用特定的方式,简化了spring的各种xml配置文件,并通过maven或者gradle,完成所需依赖,使用springboot maven插件,可直接输出可运行的jar包,省去了 ...

  3. Excle 导入DataSet

    using System.Data.OleDb;using System.Data; public void ReadExcelFiless()        {            //strin ...

  4. C# this关键字的四种用法(转)

    用法一  this代表当前类的实例对象 namespace Demo { public class Test { private string scope = "全局变量"; pu ...

  5. java使用poi.3.10读取excel 2003 (xls格式)

    最近在做一个Excel导入数据库的案例,整理文档出来供大家参考. 1.下载 最新的 poi http://poi.apache.org/download.html    2.解压 把相关jar包引进项 ...

  6. C#基础知识-引用类型和值类型的区别(六)

    在第一篇中我们介绍了C#中基本的15种数据类型,这15种数据类型中又分为两大类,一种是值类型,一种是引用类型.值类型有sbyte.short.long.int.byte.ushort.uint.ulo ...

  7. mysql安装与使用

    一.Mysql官方下载地址:https://www.mysql.com/downloads/ 二.下载 Community Server,这个版本是完全免费的  https://dev.mysql.c ...

  8. MySQL8.0加载文件内容报错: ERROR 1148: The used command is not allowed with this MySQL version

    mysql数据库将文件内容加载到表中报错: mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet LINES TERMINAT ...

  9. HTML标签 随笔2

    3-1  使用ul   添加新闻信息列表 ul-li是没有前后顺序的信息列表. 语法: <ul> <li>信息</li> <li>信息</li&g ...

  10. android launcher

    1.android_launcher的源码详细分析 2.android---launcher 3.Android 4.4 Launcher3桌面源码分析 4.通过深度剖析Android之Launche ...