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 (≤). 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

题意:

给出二叉搜索树的一个序列,如果这棵树还要求是完全二叉树的话,这棵树就是唯一的。现在要输出这棵树的层序遍历。

题解:

看了别人的代码,也不是很能理解。。。。

一棵排序二叉树的中序遍历就是这一组数的递增序列。

这边是完全二叉树,假设从0开始,那么节点i的左孩子的标号就是2*i+1,右孩子的标号就是2*(i+1)。

先将这组数按照递增来排序,然后用中序遍历复原这棵完全排序二叉树,最后直接输出。

完全二叉排序树按层序存放在从1开始的数组中,左右儿子节点的索引分别为(根节点索引假设为root)root*2和root*2+1。而二叉排序树的中序遍历是递增的。我们现在把输入序列排序,就可以得到中序遍历的序列了。于是我们开始遍历这棵左右儿子节点的索引分别为(根节点索引假设为root)root*2和root*2+1的空树,与以往一边遍历一边打印不同的是,我们是一边遍历,一边给这棵空树赋值
很不错。学习这种思想。

AC代码:

#include<iostream>
#include<set>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
int n, a[], b[], k = ;
void inorder(int root)
{
//cout<<"root:"<<root<<endl;
if (root<n)
{
inorder( * root + );
//printf("%d ==== %d ==== %d\n",root,k,a[k]);
b[root] = a[k++];
inorder( * root + );
}
} int main()
{
cin >> n;
for (int i = ; i<n; i++)
cin >> a[i];
sort(a, a + n); //从小到大排序 因为完全二叉搜索树的中序遍历就是从小到大排序
inorder();
for (int i = ; i<n - ; i++)
cout << b[i] << " ";
cout << b[n-] << endl;
return ;
}


root:
root:
root:
root:
root:
==== ====
root:
==== ====
root:
root:
==== ====
root:
==== ====
root:
root:
root:
==== ====
root:
==== ====
root:
==== ====
root:
root:
root:
==== ====
root:
==== ====
root:
root:
==== ====
root:

PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)的更多相关文章

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

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

  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. 十二.Protobuf3编码

    本文档描述了协议缓冲消息的二进制格式.在应用程序中使用Protocol Buffer不需要理解这一点,但是了解不同的Protocol Buffer格式如何影响编码消息的大小会非常有用. 一条简单的信息 ...

  2. 织梦关于表情符后面跟着css样式修改

    织梦关于表情符后面跟着css样式修改,前段页面是会员中心里,后台是会员心情管理 前段修改路径找到member\index_do.php  大约在396行,找不到搜索  ‘对表情进行解析’  替换下面的 ...

  3. Java【基础学习】之暴力求素数【用数组返回】

    Java[基础学习]之暴力求素数[用数组返回] */ import java.util.*; public class Main{ public static void main(String[] a ...

  4. 自用java购物

    @RequestMapping("listgoodscart") public ResultEntity listGoodsCart(@RequestParam(name = &q ...

  5. 生活 RH阴性血 AB型

    这个血型很稀有,外国多些,中国很少. ABO型:A.B.AB.O RH血型系统:阴性,阳性 RH阴性血,被称为熊猫血,估计是稀有吧,阴性血缺抗D,我老婆的血型抗原好像是:ccee,大部分汉族人都有抗D ...

  6. linux ftp启用和停用及vsftpd安装 ssh 启用和停用命令及ssh的安装

    1.首页要用ftp:服务器上必须安装vsftpd 安装命令 yum installed vsftpd 查看vsftpd是否启动 root@DK:/home/daokr# service vsftpd ...

  7. 2017.10.5 国庆清北 D5T1 拼不出的数

    题目描述 3 个元素的集合{5,1,2}的所有子集的和分别是0,1,2,3,5,6,7,8.发现最小的不能由该集合子集拼出的数字是4. 现在给你一个n个元素的集合,问你最小的不能由该集合子集拼出的数字 ...

  8. vue-cli3配置多页面入口

    假如要单独将登陆页面当成一个项目入口文件: 第一步:创建一个登陆页面的文件 在项目public文件夹下创建一个login.html,其实就是将index.html复制一份,将title改一下: 第二步 ...

  9. border-radius后面写px/rem与百分比有什么区别?

    首先百分比,表示的是设置50%表示的是圆是弧度,设置px/rem,是表示你想要变圆弧的半径是多少

  10. pyinstaller在64位系统下打包32位程序

    使用环境说明:win10 64位,已安装python3.6-64位版本 遇到的问题:win10 64位打包成exe文件后,不能在32位系统运行 需求:使用python打包生成exe文件,win64位和 ...