PAT 甲级 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
题意:
给出二叉搜索树的一个序列,如果这棵树还要求是完全二叉树的话,这棵树就是唯一的。现在要输出这棵树的层序遍历。
题解:
看了别人的代码,也不是很能理解。。。。
一棵排序二叉树的中序遍历就是这一组数的递增序列。
这边是完全二叉树,假设从0开始,那么节点i的左孩子的标号就是2*i+1,右孩子的标号就是2*(i+1)。
先将这组数按照递增来排序,然后用中序遍历复原这棵完全排序二叉树,最后直接输出。
完全二叉排序树按层序存放在从1开始的数组中,左右儿子节点的索引分别为(根节点索引假设为root)root*2和root*2+1。而二叉排序树的中序遍历是递增的。我们现在把输入序列排序,就可以得到中序遍历的序列了。于是我们开始遍历这棵左右儿子节点的索引分别为(根节点索引假设为root)root*2和root*2+1的空树,与以往一边遍历一边打印不同的是,我们是一边遍历,一边给这棵空树赋值。
很不错。学习这种思想。
AC代码:
#include<iostream>
#include<set>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
int n, a[], b[], k = ;
void inorder(int root)
{
//cout<<"root:"<<root<<endl;
if (root<n)
{
inorder( * root + );
//printf("%d ==== %d ==== %d\n",root,k,a[k]);
b[root] = a[k++];
inorder( * root + );
}
} int main()
{
cin >> n;
for (int i = ; i<n; i++)
cin >> a[i];
sort(a, a + n); //从小到大排序 因为完全二叉搜索树的中序遍历就是从小到大排序
inorder();
for (int i = ; i<n - ; i++)
cout << b[i] << " ";
cout << b[n-] << endl;
return ;
}
root:
root:
root:
root:
root:
==== ====
root:
==== ====
root:
root:
==== ====
root:
==== ====
root:
root:
root:
==== ====
root:
==== ====
root:
==== ====
root:
root:
root:
==== ====
root:
==== ====
root:
root:
==== ====
root:
PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)的更多相关文章
- pat 甲级 1064. 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分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 2019ICPC徐州网络赛 A.Who is better?——斐波那契博弈&&扩展中国剩余定理
题意 有一堆石子,两个顶尖聪明的人玩游戏,先取者可以取走任意多个,但不能全取完,以后每人取的石子数不能超过上个人的两倍.石子的个数是通过模方程组给出的. 题目链接 分析 斐波那契博弈有结论:当且仅当石 ...
- python - Django 跨域配置
一:settings 中间件配置路径 MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.s ...
- python链接oracle数据库以及数据库的增删改查实例
初次使用python链接oracle,所以想记录下我遇到的问题,便于向我这样初次尝试的朋友能够快速的配置好环境进入开发环节. 1.首先,python链接oracle数据库需要配置好环境. 我的相关环境 ...
- Greenplum failed segment的恢复方法--primary与mirror都可修复
当在使用greenplum过程中有不当的操作时,可能会出现segment节点宕掉的情况(比如在greenplum运行的过程中停掉其中几台segment节点的服务器),通过下面的方法可以恢复segmen ...
- Kubernetes 学习7 Pod控制器应用进阶2
一.容器探测器 1.所谓的容器探测无非就是我们在里面设置了一些探针,或者称之为传感器来获取相应的数据作为判定其存活与否或就绪与否的标准,目前k8s所支持的存活性和就绪性探测方式都是一样的. 2.k8s ...
- webuploader+php如何实现分片+断点续传
这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数 下面直接贴代码吧,一些难懂的我大部分都加上注释了: 上传文件实体类: 看得 ...
- cube.js 学习(七)cube.js type 以及format 说明
cube.js 对于measure以及dimension 提供了丰富的数据类型,基本满足我们常见应用的开发,同时对于不同类型也提供了 格式化的操作 measure类型 number 格式 purc ...
- [分享]Passcape Software - Windows Password Recovery
[分享]Passcape Software - Windows Password Recovery https://bbs.pediy.com/thread-245965.htm [[other] ...
- [golang]写了一个可以用 go 来写脚本的工具:gosl
转自:https://golangtc.com/t/53cca103320b52060a000030 写了一个可以用 go 来写脚本的工具:gosl 代码和使用说明可以看这里: http://gith ...
- SSM ehcache 配置 mapper 文件出错
异常 十二月 26, 2017 1:44:49 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertie ...