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

题干

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

思路

抓住两个点:

  • BST的中序历遍的结果是有序的。
  • 用数组建CBT可以完全利用空间,不必考虑具体的长度问题,

我们利用这两个点,将原数据进行排序后,递归建立一个数组树。建立好之后,数组的结果就是层序历遍的结果。

code

#include <iostream>
#include <algorithm>
#include <vector>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int cnt = 0;
void dfs(vector<int> &order, vector<int> &tree, int index){
if(index >= order.size()) return;
dfs(order, tree, index * 2 + 1);
tree[index] = order[cnt++];
dfs(order, tree, index * 2 + 2);
}
int main(int argc, char** argv) {
int n = 0;
scanf("%d", &n);
vector<int> order(n), tree(n);
for(int i = 0; i < n; i++) scanf("%d", &order[i]);
sort(order.begin(), order.end());
dfs(order, tree, 0);
for(auto it = tree.begin(); it != tree.end(); it++){
if(it != tree.begin()) printf(" ");
printf("%d", *it);
}
return 0;
}

PAT甲级:1064 Complete Binary Search Tree (30分)的更多相关文章

  1. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

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

  2. pat 甲级 1064. Complete Binary Search Tree (30)

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

  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 甲级 1064 Complete Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...

  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. 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)

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

  7. 【PAT甲级】1064 Complete Binary Search Tree (30 分)

    题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...

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

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

  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. 使用PCAST检测散度以比较GPU和CPU结果

    使用PCAST检测散度以比较GPU和CPU结果 并行编译器辅助软件测试(PCAST)是英伟达HPC FORTRAN.C++和C编译器中的一个特性.PCAST有两个用例.一个新的处理器或新的编译程序的部 ...

  2. MySQL泛泛而谈(3W字)

    下面对于MySQL进行相关介绍,文档的内容较为基础,仅仅设计操作,少量原理,大佬请绕道哦. 废话少说,开冲! 一.MySQL架构介绍 1-MySQL简介 概述 MySQL是一个关系型数据库管理系统,由 ...

  3. Nginx为什么快到根本停不下来?

    Nginx 是一个免费的,开源的,高性能的 HTTP 服务器和反向代理,以及 IMAP / POP3 代理服务器. 图片来自 Pexels Nginx 以其高性能,稳定性,丰富的功能,简单的配置和低资 ...

  4. redis为什么要提供pipeline功能

    通常我们用redis做接口缓存后,查询接口的性能就能提升到ms级别: 但是redis是纯内存操作啊,总不至于要到ms吧,根据官方的 benchmark 单实例也是能抗 7w+ qps 也就是说单个re ...

  5. Redis:我是如何与客户端进行通信的

    江湖上说,天下武功,无坚不摧,唯快不破,这句话简直是为我量身定制. 我是一个Redis服务,最引以为傲的就是我的速度,我的 QPS 能达到10万级别. 在我的手下有数不清的小弟,他们会时不时到我这来存 ...

  6. Linux网络基础TCP/IP

    1.osi:七层 上三层,主要是用户层面;下四层是实际进行数据传输物理层: 设备之间比特流的传输,物理接口,电气特性等 端口号的作用 通过IP找到服务器,通过端口号找到具体哪个服务.网页服务的端口号是 ...

  7. Java并发编程中的锁

    synchronized 使用synchronized实现同步有2种方式: 同步方法(静态与非静态) 同步代码块 任何Java对象均可作为锁使用,其中,使用的锁对象有以下3种: 静态同步方法中,锁是当 ...

  8. Redis配置统计字典

    本章将对Redis的系统状态信息(info命令结果)和Redis的所有配置(包括Standalone.Sentinel.Cluster三种模式)做一个全面的梳理,希望本章能够成为Redis配置统计字典 ...

  9. 面向.NET开发人员的Dapr- actors 构建块

    原文地址:https://docs.microsoft.com/en-us/dotnet/architecture/dapr-for-net-developers/actors The actor m ...

  10. 使用Flex实现图片旋转。

    当用flex实现图片旋转的时候,遇到了这样的问题:截图之后,图片还是会继续旋转,应该是canvas这个还有旋转的角度,所以看到效果跟你截图保存下来的效果不一样. 函数: 角度转换为弧度,这里面涉及到了 ...