1064. Complete Binary Search Tree (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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代码:
#define _CRT_SECURE_NO_DEPRECATE
#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<cctype>
#include<cmath>
#include<cstring>
#include<vector>
#include<set>
#include<queue>
#include<limits.h>
using namespace std;
typedef long long ll;
#define N_MAX 1000+5
#define INF 0x3f3f3f3f
int n, a[N_MAX];
struct Node {
int key;
int l_child, r_child;
Node(int key=INF,int l_child=INF,int r_child=INF):key(key),l_child(l_child),r_child(r_child) {}
}node[N_MAX];
void init(int n) {//建立完全二叉树的框架
n--;
int cur = ;
int flag = ;
while (n--) {
if (!(flag & ))node[cur].l_child = * cur + ;
else {
node[cur].r_child = *cur+;
cur++;
}
flag++;
}
} int cnt = ;
void inorder(int n) {
if(node[n].l_child!=INF)inorder(node[n].l_child);
node[n].key = a[cnt++];
if(node[n].r_child!=INF)inorder(node[n].r_child);
}
vector<int>vec;
void bfs() {
queue<int>que;
que.push();
while (!que.empty()) {
int p = que.front(); que.pop();
vec.push_back(node[p].key);
if (node[p].l_child != INF)que.push(node[p].l_child);
if (node[p].r_child != INF)que.push(node[p].r_child);
}
}
int main() {
while (scanf("%d", &n) != EOF) {
for (int i = ; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a,a+n);
init(n);
cnt = ;
inorder();
bfs();
for (int i = ; i < vec.size();i++) {
printf("%d%c",vec[i],i+==vec.size()?'\n':' ');
}
}
return ;
}

pat 甲级 1064. Complete Binary Search Tree (30)的更多相关文章

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

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

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

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

  3. PAT 甲级 1064 Complete Binary Search Tree

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

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

  5. PAT甲级:1064 Complete Binary Search Tree (30分)

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

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

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

  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甲级——A1064 Complete Binary Search Tree

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

  9. 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)

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

随机推荐

  1. shell数组脚本

    #!/bin/bash array=( ) ;i<${#array[*]};i++)) do echo ${array[i]} done 脚本2 #!/bin/bash array=( ) fo ...

  2. PHP导出成PDF功能开发教程

    准备工作 首先查询了相关的类库,有FPDF,zendPDF,TcPDF等等.首先看了下先选择了FPDF,可以说除了中文字符以外没有什么问题,中文乱码而且看了下最新版本没有很好的解决方案,所以只能放弃. ...

  3. tcl之string操作-match/map/大小写转换

  4. 什么是redis缓存穿透, 缓存雪崩, 缓存击穿

    什么是redis? redis是一个非关系型数据库,相对于其他数据库而言,它的查询速度极快,且能承受的瞬时并发量非常的高.所以常常被用来存放网站的缓存,以减少主要数据库(如mysql)的服务器压力. ...

  5. C、C++混合调用——博客收藏

    C与C++接口相互调用:https://www.cnblogs.com/feige1314/p/7890982.html C.C++混合调用:https://www.cnblogs.com/xuany ...

  6. CCPC 2016-2017, Finals

    A. HDU 5999 The Third Cup is Free 简单模拟. B. HDU 6000 Wash n 件衣服, m 个洗衣机,k 个烘干机.每个洗衣机和烘干机需要不同的时间.问 n 件 ...

  7. 51nod_1199 树的先跟遍历+区间更新树状数组

    题目是中文,所以不讲题意 做法顺序如下: 使用先跟遍历,把整棵树平铺到一维平面中 使用自己整的区间更新树状数组模板进行相关操作. http://www.cnblogs.com/rikka/p/7359 ...

  8. loj2065 「SDOI2016」模式字符串

    ref不是太懂 #include <iostream> #include <cstring> #include <cstdio> using namespace s ...

  9. IOS开发学习笔记031-代码实现微博界面

    微博界面如下 1.准备资源文件 新建一个plist文件,添加条目,root类型是array,子类型是Dictionary 2.更改父类,实现代理方法 接下来得实现过程如上一篇文章,改变父类为UITab ...

  10. 微信小程序-----校园头条详细开发之列表展示数据

    1.分类列表数据展示功能的实现 1.1 结构 1.2 代码实现 1.2.1  列表显示数据,.每次界面显示6条数据,发请求获取数据,动态存放 var app = getApp() Page({ dat ...