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. C#(Winform)的SaveFileDialog(文件保存对话框)控件使用

       #region 保存对话框   private void ShowSaveFileDialog()   {         //string localFilePath, fileNameExt ...

  2. DOM的概念和简单应用:使用DOM解析XML数据

    概念:DOM是Document Object Model的简称,即文档数据模型. Oracle公司提供了JAXP(Java API for XML Processing)来解析XML.JAXP会把XM ...

  3. js原生拖拽

    style样式 <style type="text/css"> #box{ width: 100px; height: 100px; background: deepp ...

  4. Stage2--Python的数据类型

    说在前面: Stage1-Stage4简单介绍一下Python语法,Stage5开始用python实现一些实际应用,语法的东西到处可以查看到,学习一门程序语言的最终目的是应用,而不是学习语法,语法本事 ...

  5. centos的nginx支持ssl

    首先看centos是否支持ssl 输入:openssl version 如无 则去 http://slproweb.com/products/Win32OpenSSL.html  寻找 生成私钥后面的 ...

  6. Python对数组的基本操作

    # coding=utf-8创建并打印数组'''arr = ["aex", "bfe", "mpilgrim", "zddd&qu ...

  7. Service Broker完成实例之间的会话详细解读

    首先了解service broker是什么东西: Service Broker 是数据库引擎的组成部分,因此管理这些应用程序就成为数据库日常管理的一部分. Service Broker 为 SQL S ...

  8. 会话cookie中缺少HttpOnly属性 解决

    会话cookie中缺少HttpOnly属性 解决   只需要写一个过滤器即可 1 package com.neusoft.streamone.framework.security.filter; 2 ...

  9. t d x 示例z

    using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServi ...

  10. UIRecorder安装与使用

    继vue单元测试,将进行vue的e2e测试学习. 学习点: 安装uirecorder 用工具(UI Recorder)录制测试脚本 测试脚本的回放 本文意在安装UI Recorder,并且利用该工具进 ...