题目信息

1064. Complete Binary Search Tree (30)

时间限制100 ms

内存限制65536 kB

代码长度限制16000 B

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

解题思路

模拟建树推出层序

AC代码

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int n, a[1005];
vector<int> level[22];
void step(int loc, int len, int lv){
if (len <= 0) return;
int t = 1;
while (t*2 <= len) t *= 2;
--t;
int cd = ((len - (len - t)) + 1) / 2 + len - t; if (cd <= t + 1){
level[lv].push_back(a[loc + cd - 1]);
step(loc, cd - 1, lv + 1);
step(loc + cd, len - cd, lv + 1);
}else{
level[lv].push_back(a[loc + t]);
step(loc, t, lv + 1);
step(loc + t + 1, len - t - 1, lv + 1);
}
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; ++i){
scanf("%d", a+i);
}
sort(a, a + n);
step(0, n, 0);
printf("%d", level[0][0]);
for (int i = 1; i <= 13; ++i){
for (int j = 0; j < level[i].size(); ++j){
printf(" %d", level[i][j]);
}
}
printf("\n");
}

个人游戏推广:

apkName=com.xianyun.yf" target="_blank" align="left">

apkName=com.xianyun.yf" target="_blank" align="left">《10云方》与方块来次消除大战!

1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise的更多相关文章

  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 (30分)

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

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

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

  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)-中序和层次遍历,水

    由于是满二叉树,用数组既可以表示父节点是i,则左孩子是2*i,右孩子是2*i+1另外根据二分搜索树的性质,中序遍历恰好是从小到大排序因此先中序遍历填充节点对应的值,然后再层次遍历输出即可. 又是一道遍 ...

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

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

  9. PAT (Advanced Level) 1064. Complete Binary Search Tree (30)

    因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include& ...

随机推荐

  1. Java:一个简捷的可分页的ResultSet实现

    内容 前言 JDBC和分页 和具体数据库相关的实现方法 另一种繁琐的实现方法 使用Vector进行分页 一个新的Pageable接口及其实现 Pageable的使用方法 总结 参考资料 关于作者 前言 ...

  2. cookie的应用——浏览记录

    实体类 package entity; public class Product { private String id; private String proName; private String ...

  3. Java 基础入门随笔(4) JavaSE版——程序流程控制

    上一节对于运算符有了大致的了解,这一节针对程序流程控制进行复习!程序流程控制包括顺序结构.判断结构(if).选择结构(switch).循环结构. 1.判断结构 ①if语句的第一种格式:        ...

  4. MySql学习笔记(四) —— 数据的分组

    前面介绍的聚集函数只是用来计算行数,平均数,最大值,最小值而不用检索所有数据.通过count()函数,我们可以计算生产商1003提供的产品数目,但如果我要查询所有生产商提供的商品数,这就需要进行分组查 ...

  5. 12C语言标准函数库

    C语言标准函数库 数学函数 三角函数 指数和对数函数 双曲线函数 其它函数 Sqrt() Pow() Exp() Log() Sin() Cos() Tan() 时间函数 查找和排序 Bsearch( ...

  6. linux cp复制文件 直接覆盖

    命令: \cp -rf aaaa/* bbbb 复制aaa下的文件到bbb目录

  7. NOIP 前的垂死挣扎

    计划每天十题吧,可能会一天水题一天难题吧.题目以杂题为主,没有专题可言. 10.11 计划: [x] P2939 [USACO09FEB] 改造路 Revamping Trails [ ] P3601 ...

  8. MyBatis 的基本要素—核心配置文件

    MyBatis 核心配置文件( mybatis-config.xml),该文件配置了 MyBatis 的一些全局信息,包含数据库连接信息和 MyBatis 运行时所需的各种特性,以及设置和影响 MyB ...

  9. Datatable 导出到execl 官网demo

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...

  10. type="timestamp"与type="date"区别

    type="timestamp"-----数据库中保存的时间为年月日时分秒 与type="date"---------数据库中保存的时间为年月日