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<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int tree[], N, index = , num[];
bool cmp(int a, int b){
return a < b;
}
void inOrder(int root){
if(root > N)
return;
inOrder(root * );
tree[root] = num[index++];
inOrder(root * + );
}
int main(){
scanf("%d", &N);
for(int i = ; i < N; i++)
scanf("%d", &num[i]);
sort(num, num + N, cmp);
inOrder();
for(int i = ; i <= N; i++){
if(i != N)
printf("%d ", tree[i]);
else printf("%d", tree[i]);
}
cin >> N;
return ;
}

总结:

1、题意:给出一组数字,要求将它们建立成一颗二叉搜索树。因为结果不唯一,所以加了限制条件:要求搜索树是一颗完全二叉树。

2、二叉搜索树的中序序列是从小到大的有序数列,所以对初始序列排序后就能得到搜索树的中序序列

3、完全二叉树的树形状在给出节点个数N之后就是已知的。所以相当于已经知道了答案所求树的形状,但仅仅是树的节点没有填入值罢了。由于搜索树的中序序列已知,只需要按照中序遍历完全二叉树,在遍历的过程中填入搜索树的中序序列值即可。

A1064. Complete Binary Search Tree的更多相关文章

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

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

  2. PAT_A1064#Complete Binary Search Tree

    Source: PAT A1064 Complete Binary Search Tree (30 分) Description: A Binary Search Tree (BST) is recu ...

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

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

  4. 04-树5 Complete Binary Search Tree

    这题也是第二次做,本想第一次做时参考的算法会和老师讲的一样,不想老师讲的算法用在这题感觉还不如思雪园友的算法(http://www.cnblogs.com/sixue/archive/2015/04. ...

  5. 04-树6 Complete Binary Search Tree

    完全二叉树 刚开始只发现了中序遍历是从小到大顺序的.一直在找完全二叉树的层结点间规律...放弃了 不曾想,完全二叉树的规律早就知道啊.根结点为i,其左孩子结点2*i, 右孩子结点2*i+1. 结合此两 ...

  6. Complete Binary Search Tree

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

  7. 04-树6 Complete Binary Search Tree(30 分)

    title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...

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

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

随机推荐

  1. peewee 事物 回滚

    peewee 事物 回滚 #!/usr/bin/env python # coding=utf-8 from peewee import * db = MySQLDatabase(host='123. ...

  2. for 循环增强

    package cn.zhou.com; /* * 增强for循环 * * for(int i:arr) * { * System.out.print(i+1+" "); * } ...

  3. JS实现控制HTML5背景音乐播放暂停

    首先在网页中嵌入背景音乐,html5代码为: <script src="http://wuover.qiniudn.com/jquery.js"></script ...

  4. 将自己的ubuntu18.04打包成镜像

    将自己的ubuntu18.04打包成镜像 2018年11月10日 10:40:06 舌耳 阅读数:1590 先下载remastersys wget ftp://ftp.gwdg.de/pub/linu ...

  5. sklearn训练感知器用iris数据集

    简化版代码 from sklearn import datasets import numpy as np #获取data和类标 iris = datasets.load_iris() X = iri ...

  6. js中session操作

    // 保存数据到sessionStorage sessionStorage.setItem('key', 'value');   // 从sessionStorage获取数据 var data = s ...

  7. VM下安装Kali虚拟机

    VM下Kali虚拟机安装 下载kali Linux系统镜像 下载地址:http://mirrors.hust.edu.cn/kali-images/ 网页如下: kali官网:http://www.k ...

  8. Redis——windows下如何连接Linux(centos7.x)虚拟机的Redis——【二】

    我的虚拟网络使用的是桥接网络和windows主机IP为同一网段,做下面步骤之前请确保网络通畅. 使用cmd的ping来测试 软件 https://redisdesktop.com/download 下 ...

  9. 洛谷P2918 [USACO08NOV]买干草(一道完全背包模板题)

    题目链接 很明显的一道完全背包板子题,做法也很简单,就是要注意 这里你可以买比所需多的干草,只要达到数量就行了 状态转移方程:dp[j]=min(dp[j],dp[j-m[i]]+c[i]) 代码如下 ...

  10. HUST 1555 数学作业

    参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6394892.html 1555 - A Math Homework 时间限制:1秒 内存限制:12 ...