pat 甲级 1064. Complete Binary Search Tree (30)
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 (<=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)的更多相关文章
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binar ...
- PAT 甲级 1064 Complete Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...
- 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 ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 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 ...
- PAT甲级——A1064 Complete Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- 1503: [NOI2004]郁闷的出纳员
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 13723 Solved: 4989[Submit][Status][Discuss] Descripti ...
- 多页应用 Webpack4 配置优化与踩坑记录
前言 最近新起了一个多页项目,之前都未使用 webpack4 ,于是准备上手实践一下.这篇文章主要就是一些配置介绍,对于正准备使用 webpack4 的同学,可以做一些参考. webpack4 相比之 ...
- nginx的缓存服务
都知道缓存的目的是为了减小服务端的压力,可以在客户端直接取到数据 客户端---------------nginx(代理缓存)------------------服务端 代理缓存的描述: 就是客户端发送 ...
- HDU:2594-Simpsons’ Hidden Talents
Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 初识Java之入门学习(扫盲)
一,开发环境的配置 1. jdk1.8的安装 2. 环境变量的配置 3.MyEclipse8.5的安装 jdk是什么: JDK 是Java开发工具包 (Java Development Kit ) 的 ...
- sql中一个服务器建立另一个服务器的连接
EXEC sp_addlinkedserver 'TonyLink','','SQLOLEDB','111.111.1.111(服务器名)' EXEC sp_addlinkedsrvlogin 'To ...
- windows server 2008解决无法PING通问题
今天安装服务器(server 2008),配置完IP地址后,发现局域网其它电脑无法PING通服务器,测线仪测试链路都正常,网线接别的电脑也正常,以为是网卡问题,于是ping了自己的IP,发现能PING ...
- 爬虫工程师常用的 Chrome 插件
做多了爬虫都知道,写一个爬虫大部分时间不是在代码上,而是在分析网页上,所有有一套好用的工具可以极大节省劳动力,这里把平时积累的一些 Chrome 插件分享出来,均来自本人和同事推荐,并不定时更新,欢迎 ...
- SetUnhandledExceptionFilter
SetUnhandledExceptionFilter 设置未捕获异常处理 通常windows程序长时间运行,会发生各种问题,例如访问异常,内存溢出,堆栈破坏等. 这时候通常希望程序自己能增加处理,而 ...
- 手机APP设计网
http://hao.xueui.cn/ http://www.25xt.com/