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. 2017年9月22日作业 c++算术运算符 自增 自减 逻辑运算符 位运算符 条件运算符(三元运算符)

    作业1: c++算术运算符试题,分析下面程序的输出结果是什么 //第一个: int x=8999;int value=x*1000/1000; //第二个 int x=8999;int value=x ...

  2. 数据结构(C语言)分享笔记:数据结构的逻辑层次、存储层次

    [1] 严格意义上数据结构的概念 数据结构,一个简单的定义:相互之间存在一种或多种特定关系的数据元素的集合.即:数据结构 = 元素集合 + 元素间关系的集合 . 在讨论数据结构时,可以基于两个不同的层 ...

  3. bat 服务启动脚本

    当电脑上有多个数据库(特别是Oracle,占用内存大,所以我都是设置为手动启动的,或者想在电脑上运行一下其他UI类软件或玩些游戏的时候也需要暂时关掉,奈何我这渣机(V_V))需要启动或停止的时候,就用 ...

  4. 认识mysql(2)

    1.表字段的操作 1.语法 :alter table 表名 执行动作; 2.添加字段(add) alter table 表名 add 字段名 数据类型; alter table 表名 add 字段名 ...

  5. 神经网络系列学习笔记(二)——神经网络之DNN学习笔记

    一.单层感知机(perceptron) 拥有输入层.输出层和一个隐含层.输入的特征向量通过隐含层变换到达输出层,在输出层得到分类结果: 缺点:无法模拟稍复杂一些的函数(例如简单的异或计算). 解决办法 ...

  6. JZOJ 3461. 【NOIP2013模拟联考5】小麦亩产一千八(kela)

    3461. [NOIP2013模拟联考5]小麦亩产一千八(kela) (Standard IO) Time Limits: 1000 ms  Memory Limits: 262144 KB  Det ...

  7. Redis之String类型操作

    接口IRedisDaoStr: package com.net.test.redis.base.dao; import java.util.List; import java.util.Map; /* ...

  8. Android 服务入门

    前言:硬着头皮把数据库SQLite看完了,接下来就是android服务了,因为自己本身就是菜鸟,所以呢,也只是做做笔记,技术上的东西就别指望我了. 1.什么是服务呢?举个例子,百度地图,美团外卖,OF ...

  9. pyautogui 模块学习

    在日常实施中,我们用控件对大部分的网页和客户端都能进行拾取操作.但是仍有一小部分的应用无法进行操作.这里我常用到 pyautogui 这个模块.下面做个分享. Python 的 pyautogui 模 ...

  10. MySQL基础7-分页查询

    1.分页查询(MySQL特有的,oracle中没有) 栗子1: 每页最多3条记录:pageSize=3:第一页:SELECT * FROM product LIMIT 0,3第二页:SELECT * ...