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. TOJ 1690 Cow Sorting (置换群)

    Description Farmer John's N (1 ≤ N ≤ 10,000) cows are lined up to be milked in the evening. Each cow ...

  2. TOJ 2452 Ultra-QuickSort

    描述 In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a se ...

  3. 【转】Python中不尽如人意的断言Assertion

    原文地址:Python中不尽如人意的断言Assertion Python Assert 为何不尽如人意 Python中的断言用起来非常简单,你可以在assert后面跟上任意判断条件,如果断言失败则会抛 ...

  4. BNU27945——整数边直角三角形——————【简单数学推导】

    整数边直角三角形 Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class nam ...

  5. SpringBoot | 第三十二章:事件的发布和监听

    前言 今天去官网查看spring boot资料时,在特性中看见了系统的事件及监听章节.想想,spring的事件应该是在3.x版本就发布的功能了,并越来越完善,其为bean和bean之间的消息通信提供了 ...

  6. SQL Exists 的用法 转载

    比如在Northwind数据库中     有一个查询为 SELECT c.CustomerId, CompanyName FROM Customers c WHERE EXISTS( SELECT O ...

  7. 1、操作表 ActionSheet

    /* --- page1.html ---*/ <ion-navbar *navbar> <ion-title>Tab 1</ion-title> </ion ...

  8. C#-01.语法基础

    a. 语法基础 i. 命名空间(namespace):是 C# 中组织代码的方式,用来声明命名空间 . 语法:namespace 命名空间名称{ //命名空间的声明 } . 作用:可以把紧密相关的一些 ...

  9. 拼凑的宿主-host

    开发两年之久,竟然不知道host这个词是什么意思.前些天有幸遇到了,就跟别人请教了.今天理絮一下.总比不知道强吧. 白话来说宿主就是一些框架运行机制运行时需要依赖的平台. 例如web开发常用的IIS, ...

  10. RestTemplate请求出现401错误

    最近遇到一个请求API接口总是报401 Unauthorized错误,起初是认为这个是平台返回的,后来用Postman请求,发现平台其实返回的是一串json,里面带有一些权限验证失败的消息,但到我们代 ...