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. WPF如何实现TreeView节点重命名

    我们经常看到一些软件比如酷狗音乐,在对列表右键进行重命名的时候,当前列表会泛白并且进入可编辑状态,当我们更改完成后就会并进入非编辑状态,这些具体是怎么实现的呢?下面的方法也许会提供一些思路,下面的Tr ...

  2. Python——数组模块(array)

    一.模块说明 array模块是python中实现的一种高效的数组存储类型.它和list相似,但是所有的数组成员必须是同一种类型,在创建数组的时候,就确定了数组的类型. 二.代码

  3. case when 空值判断

    在对数据库进行查询时,遇到了一个问题:查询结果中的某一列需要判断另一列是否为空的来确定值,自然就想到了case when,于是写出了下面的SQL(其他部分省略): (case date when nu ...

  4. ES 6 系列 - 变量声明

    let 和 const let 声明 (一)基本用法 let 声明的变量只在块级作用域内有效,出了该块则报错,最常见且适合的地方在 for 循环中: var a = []; for (var i = ...

  5. Windows 7 quick launch

    %AppData%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar %AppData%\Microsoft\Internet ...

  6. 树&图 记录

    A - Lake Counting POJ - 2386 最最最最最基础的dfs 挂这道题为了提高AC率(糖水不等式 B - Paint it really, really dark gray Cod ...

  7. 【数学建模】day02-整数规划

    基本类似于中学讲的整数规划--线性规划中变量约束为整数的情形. 目前通用的解法适合整数线性规划.不管是完全整数规划(变量全部约束为整数),还是混合整数规划(变量既有整数又有实数),MATLAB都提供了 ...

  8. [BZOJ 2186] [SDOI 2008] 沙拉公主的困惑

    Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为 \(1\) 到 \(N\) 的阶乘,但是,政府只发行编号与 \(M!\) 互质的钞票.房地产第 ...

  9. nginx压测工具--wrk

    基本使用 命令行敲下wrk,可以看到使用帮助 Usage: wrk <options> <url> Options: -c, --connections <N> C ...

  10. HDU1659-GCD-容斥原理

    从1-a和1-b种选两个数xy,计算出令gcd(x,y)=k的xy的对数. 对于每一个i∈[1,b]使用solve(i,n)函数解决有几个j∈[1,n]使gcd(x,y)=k.然后累加solve(i, ...