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

题目大意:给出一个构成完全二叉树(除了最后一层的右边可能缺少部分节点,其余层均达到最大节点数)的序列,要求输出该完全二叉树的层次遍历。

主要思想:根据完全二叉树的特性可以用一个数组很方便的表示出来,将根节点的索引设为1,之后每个索引为i的节点,其左节点为2i,右节点为2i+1,然后对于输入的序列进行排序后按照中序遍历依次填入二叉树数组,这样完全二叉树就构造成功了,而最后一步的层次遍历输出其实就是对数组的顺序输出。

#include <cstdio>
#include <algorithm>
int n; //节点个数
int index = 0; //序列数组的索引
int a[1005]; //输入的序列数组
int node[1005]; //完全二叉树数组
using namespace std; void travel(int i) {
if (i > n) return;
travel(2*i);
node[i] = a[index++];
travel(2*i+1);
} int main(void) {
int i; scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a, a+n);
travel(1); //中序遍历构造完全二叉树
//层次遍历输出
for (i = 1; i < n; i++)
printf("%d ", node[i]);
printf("%d\n", node[i]); return 0;
}

PAT-1064 Complete Binary Search Tree(完全二叉树)的更多相关文章

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

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

  2. PAT 1064 Complete Binary Search Tree

    #include <iostream> #include <cstdio> #include <cstdlib> #include <vector> # ...

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

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

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

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

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

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

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

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

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

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

  10. PAT 甲级 1064 Complete Binary Search Tree

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

随机推荐

  1. opencv-9-图像噪声以及评估指标 PSNR 与SSIM

    开始之前 我们在将 opencv 的图像显示在了 qt 的label 上, 我们能够将图显示在label 上, 用于显示我们的算法, 我们在 opencv 上一篇文章中介绍了 opencv 的核操作, ...

  2. 【Linux常见命令】sort命令

    sort - sort lines of text files sort命令用于将文本文件内容加以排序. sort可针对文本文件的内容,以行为单位来排序. 语法: sort [OPTION]... [ ...

  3. 【Linux常见命令】cut命令

    cut - remove sections from each line of files 参数: -b 可以按字节来查看文件中的内容 -b参数用在中文上,容易出现乱码问题.因为中文字符一个字符占两个 ...

  4. Java——Java是什么一门什么语言

    解释型语言 源代码不能直接翻译成机器语言,而是先翻译成中间代码,再由解释器对中间代码进行解释运行: 程序不需要编译,程序在运行时才翻译成机器语言,每执行一次都要翻译一次: 解释性语言代表:Python ...

  5. dp cf 20190613

    A. Boredom 这个题目不难,但是我做的还比较复杂,不过还是很开心,至少做出来了,开始因为爆int了还wa了一发,搞得我以为自己做错了 #include <cstdio> #incl ...

  6. github使用命令

    创建本地库,提交,和绑定github ,上传代码 git init git add README.md git commit -m "first commit" git remot ...

  7. jQuery的相关尺寸获取 - 学习笔记

    获取元素相对于文档的偏移量 获取当前元素相对于父级元素的偏移量 获取文档滚动距离 获取元素的宽度和高度 设置元素的宽度和高度 获取可视区域的宽度和高度 获取文档的宽度和高度 获取元素相对于文档的偏移量 ...

  8. Vue + Element-ui实现后台管理系统(1) --- 总述

    总述 一.项目效果  整体效果 登陆页 后台首页 用户管理页 说明 这里所有的数据都不是直接通过后端获取的, 而是通过Mock这个工具来模拟后端返回的接口数据. 附上github地址: mall-ma ...

  9. Day_11【集合】扩展案例3_打印最高分的学员姓名、年龄、成绩,打印10个学生的总成绩和平均分,打印不及格的学员信息及数量

    分析以下需求,并用代码实现 1.定义Student类 属性: 姓名:String name 年龄:int age 成绩:int score 行为: 空参构造方法 有参构造方法 set和get方法 to ...

  10. C:__attribute__ weak 的作用

    关于 weak weak经常出现在各种c代码中,其作用是将当前文件的对应函数声明为弱函数符号,如果外部文件出现相同的函数名,最终编译出来的 文件会优先指向外部文件的函数符号: 通常需要使用__attr ...