04-树6 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
- #include<cstdio>
- #include<algorithm>
- using namespace std;
- const int maxn = ;
- int num[maxn];
- int CBT[maxn];
- int index = ;
- void inOrder(int root, int n);
- int main()
- {
- int n;
- scanf("%d",&n);
- for (int i = ; i < n; i++)
- {
- scanf("%d",&num[i]);
- }
- sort(num,num+n);
- inOrder(,n);
- for (int i = ; i <= n; i++)
- {
- printf("%d",CBT[i]);
- if (i < n)
- {
- printf(" ");
- }
- }
- return ;
- }
- void inOrder(int root, int n)
- {
- if (root > n)
- {
- return ;
- }
- inOrder(root*, n);
- CBT[root] = num[index++];
- inOrder(root*+, n);
- }
04-树6 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 ...
- 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分)(已知中序输出层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 【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 ...
- pat04-树8. Complete Binary Search Tree (30)
04-树8. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHE ...
- pat1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 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 ...
随机推荐
- vs2017专业版和企业版的密钥
Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Professional: KBJFW-NXHK6-W4WJM-CRMQB-G3CDH
- 好用的数据库字典查看工具SQLToolbelt
工作中经常为诸多的陌生或没有任何表或者字段说明或者文档庞大数据库和数据库表所烦恼,有以下场景: 1.新进入一家公司,开始接触新的项目,领导给你一大堆文档,在不了解具体逻辑的情况下,除了项目的结构,能让 ...
- SQLServer作业调用链接服务器失败解决办法
新建一个SQL作业,语句手动执行OK,但是作业计划执行总是报错. 消息已以用户 NT SERVICE\SQLSERVERAGENT 的身份执行. 链接服务器 "172.16.10.23&qu ...
- gcc 编译过程
gcc 编译过程从 hello.c 到 hello(或 a.out)文件, 必须历经 hello.i. hello.s. hello.o,最后才得到 hello(或a.out)文件,分别对应着预处理. ...
- 2019 头条java面试笔试总结 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条等公司offer,岗位是Java后端开发,因为发展原因最终选择去了头条,入职一年时间了,也成为了面试官,之前面 ...
- spark存储管理之磁盘存储--DiskStore
DiskStore 接着上一篇,本篇,我们分析一下实现磁盘存储的功能类DiskStore,这个类相对简单.在正式展开之前,我觉得有必要大概分析一下BlockManager的背景,或者说它的运行环境,运 ...
- Beego学习笔记四:编写Model
MVC实践一:编写模型 1> 打开mysql数据库,设计表的结构 <1>登录mysql数据库,如下 <2>这三个标注的参数皆有用,需要谨记. <3>创 ...
- Mysql 游标初识
MySql 游标初识 认识 游标(cursor), 按字面意思可理解为, 游动的标识, 或者叫做"光标", 这样更容易理解. 就好比现有一张表存储了n行记录, 然后我想每次取出一行 ...
- Prometheus学习笔记(5)Grafana可视化展示
目录 一.Grafana安装和启动 二.配置数据源 三.配置dashboard 四.配置grafana告警 一.Grafana安装和启动 Grafana支持查询Prometheus.从Grafana ...
- PAT甲级1018留坑——第一个测试点未过(Dijikstar+Dfs)
题目分析: 主要是先进行狄杰斯特拉求出0点到每个点的最短路后用dfs求出所有的路径,将路径方案加入vector排序选择need最小和rest最小的方案,但是第一个测试却过不去,欢迎指正!!感谢!! 值 ...