https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568

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 N 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 <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10;
int N;
int a[maxn];
vector<int> level; void levelorder(int st, int en, int index) {
if(st > en) return ;
int n = en - st + 1;
int l = log(n + 1) / log(2);
int leave = n - (pow(2, l) - 1);
int root = st + (pow(2, l - 1) - 1) + min((int)pow(2, l - 1), leave);
level[index] = a[root];
levelorder(st, root - 1, 2 * index + 1);
levelorder(root + 1, en, 2 * index + 2);
} int main() {
scanf("%d", &N);
level.resize(N);
for(int i = 0; i < N; i ++)
scanf("%d", &a[i]); sort(a, a + N);
levelorder(0, N - 1, 0);
for(int i = 0; i < N; i ++) {
printf("%d", level[i]);
printf("%s", i != N - 1 ? " " : "\n");
}
return 0;
}

  https://www.liuchuo.net/archives/2161

还是自己太菜了 打扰了!

FHFHFH

PAT 甲级 1064 Complete Binary Search Tree的更多相关文章

  1. pat 甲级 1064. 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 (30 分)(不会做,重点复习,模拟中序遍历)

    1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a bin ...

  3. pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)

    1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binar ...

  4. PAT甲级——A1064 Complete Binary Search Tree

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

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

  6. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

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

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

  8. PAT 1064 Complete Binary Search Tree[二叉树][难]

    1064 Complete Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a b ...

  9. 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

    题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...

随机推荐

  1. kali国内更新源

    在/tec/apt/sources.list加入以下内容 #中科大更新源 deb https://mirrors.ustc.edu.cn/kali kali-rolling main non-free ...

  2. 【转】枚举enum学习小记

    原帖: http://hi.baidu.com/yuleishou/item/caacae872190031ec216272f 表示在vs2008下实验了一下,有些东西和原帖的还是不一样的,都贴在这里 ...

  3. 【转载】MFC的Main函数跑哪去了

    原文:http://blog.csdn.net/weiwenhp/article/details/8455471 习惯的思维 用习惯了C的人要看一个程序时首先会想到找到那个main函数在哪,然后再顺着 ...

  4. CF1039D You Are Given a Tree 根号分治,贪心

    CF1039D You Are Given a Tree LG传送门 根号分治好题. 这题可以整体二分,但我太菜了,不会. 根号分治怎么考虑呢?先想想\(n^2\)暴力吧.对于每一个要求的\(k\), ...

  5. SpringCloud-初识微服务(一)

    前言 本篇文章简单介绍一下什么是微服务.微服务的优点.SpringCloud的微服务架构核心组件选型等: 一.什么是微服务? 微服务的提出者Martin Fowler是这样描述微服务的(原文:http ...

  6. 2_C语言中的数据类型 (四)整数与无符号数

    1.1       sizeof关键字 sizeof是c语言关键字,功能是求指定数据类型在内存中的大小,单位:字节 sizeof与size_t类型 1.1       int类型 1.1.1      ...

  7. IDEA 出现 updating indices 卡进度条问题的解决方案并加快索引速度

    缺点: 这样的话,前端的接口(也就是字符串)就搜索不到了. C:\Users\Administrator\.IntelliJIdea2017.3\system  删除里面的caches文件夹(这里的 ...

  8. java.util.Arrays.asList 的小问题

    JDK 1.4对java.util.Arrays.asList的定义,函数参数是Object[].所以,在1.4中asList()并不支持基本类型的数组作参数. JDK 1.5中,java.util. ...

  9. leetcode刷题笔记191 位1的个数

    题目描述: 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量). 示例: 输入: 输出: 解释: 32位整数 的二进制表示为 . 题目分析: 判断3 ...

  10. k8s踩坑记第2篇--3个IP折磨人的故事

    例子来源于<Kubernetes实践指南>一书.问题依然没有解决,求助大神. 测试环境 Centos 7.0 docker 1.13.1 kubectl v1.5.2 etcd 3.2.1 ...