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. Linux 下安装 Memcached 和 PHP 开启 Memcached 扩展 及 LAMP 环境的安装

    http://blog.csdn.net/liruxing1715/article/details/8269563

  2. php高手干货【必看】

    1.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量, 单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的"函数&quo ...

  3. java XML解析成Map

    1.需要解析的文件.xml <?xml version="1.0" encoding="UTF-8"?> <request> <r ...

  4. python爬虫学习(一)

    #简单例子:抓取网页全部内容后,根据正则表达式,获取符合条件的字符串列表from urllib import request#正则表达式import re url = "http://www ...

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

  6. Shader Example

    //测试viewDir对顶点的影响Shader "Example/TestViewDir" { Properties{ _RimColor("Rim Color" ...

  7. 缩短移动开发周期的ApiCloud

    ApiCloud百度百科介绍: ApiCloud 官网 官方论坛 APICloud经典O2O案例APP视频教程

  8. EJB是什么?

    1. 我们不禁要问,什么是"服务集群"?什么是"企业级开发"? 既然说了EJB 是为了"服务集群"和"企业级开发",那么 ...

  9. spring配置连接池和dao使用jdbcTemplate

    1 spring配置c3p0连接池 第一步 导入jar包 第二步 创建spring配置文件,配置连接池 (1)把代码中的实现在配置文件中实现 2 dao使用jdbcTemplate (1) 创建ser ...

  10. Laravel 生成小程序图文海报最佳方案之一

    目前已经更新 2.0 版本,支持生成的海报关联Model,支持是否重新生成海报等功能,具体更新请移步 github: laravel-miniprogram-poster 微信小程序官方并未提供分享到 ...