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 (≤). 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
题目分析:刚开始我想的是先把末尾的多余的元素 计入计算根节点的位置 但欠缺考虑到对于k层树来说
若第k层元素大于2的k-1次 我这样的做法就出了问题
只过了2个点(21分) //给分真高
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int Array[];
int Queue[];
void EnQueue(int begin, int end,int pos) //左闭右开
{
if (begin >= end)
return;
Queue[pos] = Array[(begin + end) / ];
EnQueue(begin, (begin + end)/, * pos + );
EnQueue((begin + end) / + , end, * pos + );
}
int main()
{
int N;
cin >> N;
for (int i = ; i < N; i++)
cin >> Array[i];
sort(Array, Array + N);
int sum = ;
for (; sum * < N; sum *= );
int offset = (N - sum)/;
int i = ;
Queue[i] = Array[N / + offset]; //根节点入队
//分别递归处理左右
EnQueue(, N / + offset, * i + );
EnQueue(N / + offset + ,N, * i + );
for (int i = ; i < N - ; i++)
cout << Queue[i] << " ";
if(N!=)
cout << Queue[N- ];
return ;
}
参考别人的做法
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int Array[];
int Queue[];
int N;
int id;
void EnQueue(int root) //左闭右开
{
if (root >= N)
return;
EnQueue( * root + );
Queue[root] = Array[id++];
EnQueue( * root + );
}
int main()
{
cin >> N;
for (int i = ; i < N; i++)
cin >> Array[i];
sort(Array, Array + N);
EnQueue();
for (int i = ; i < N - ; i++)
cout << Queue[i] << " ";
cout << Queue[N - ];
return ;
}
来自https://blog.csdn.net/feng_zhiyu/article/details/82219702
果然 很多代码真的是又短又好 虽然原理简单 但体现出的思想正是让我感到震撼
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 (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- 【PAT甲级】1064 Complete Binary Search Tree (30 分)
题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- PTA 04-树6 Complete Binary Search Tree (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree (30分) A ...
- 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 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 ...
- 04-树6 Complete Binary Search Tree (30 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- Python中使用os模块执行远程命令
1. 使用os模块远程执行命令 服务端代码 1 import socket 2 import os 3 4 sh_server = socket.socket() #创建一个socket对象 5 sh ...
- kafka实现无消息丢失与精确一次语义(exactly once)处理
在很多的流处理框架的介绍中,都会说kafka是一个可靠的数据源,并且推荐使用Kafka当作数据源来进行使用.这是因为与其他消息引擎系统相比,kafka提供了可靠的数据保存及备份机制.并且通过消费者位移 ...
- ReentrantReadWriteLock 源码分析以及 AQS 共享锁 (二)
前言 上一篇讲解了 AQS 的独占锁部分(参看:ReentrantLock 源码分析以及 AQS (一)),这一篇将介绍 AQS 的共享锁,以及基于共享锁实现读写锁分离的 ReentrantReadW ...
- *fetch(_, { call, put }) { --- generator
effects: { *fetch(_, { call, put }) { const response = yield call(queryUsers); yield put({ type: 'sa ...
- 基于奇林软件kylinTOP工具的HTTP2协议的压力测试
1.HTTP协议概述 说到http,那就应该先了解一下http协议的发展历史.关于http协议的历史,可以参考阮一峰老师的这篇博客文章HTTP 协议入门,里面介绍的比较详细了.简单来说http先后存在 ...
- Spring Cloud - Nacos注册中心入门单机模式及集群模式
近几年微服务很火,Spring Cloud提供了为服务领域的一整套解决方案.其中Spring Cloud Alibaba是我们SpringCloud的一个子项目,是提供微服务开发的一站式解决方案. 包 ...
- 聊聊 WebSocket,还有 HTTP
还记得曾经风靡一时的 QQ 秀聊天室吗?那时,还在上初.高中的我们,QQ 是最常用的聊天交友工具:而 QQ 秀聊天室的出现打破了只能按条件查找好友的局限性,大家可以随意进入聊天室房间,进行在线聊天.怀 ...
- Redis启动服务和String常用命令
Redis启动服务和String常用命令 1. 启动Redis服务 E:\redis>redis-server.exe redis.windows.conf _._ _.-``__ ''-._ ...
- java web 获取 网页访问次数
ServletContext context = request.getServletContext(); /** * 从ServletContext中获取计数器对象 */Integer count ...
- 论文速递 | 实例分割算法BlendMask,实时又state-of-the-art
BlendMask通过更合理的blender模块融合top-level和low-level的语义信息来提取更准确的实例分割特征,该模型效果达到state-of-the-art,但结构十分精简,推理速度 ...