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 ...
随机推荐
- read_excle
Signature: pd.read_excel( ['io', 'sheet_name=0', 'header=0', 'skiprows=None', 'skip_footer=0', 'inde ...
- Dubbo源码分析(3):ExtensionFactory
通过ExtensionFactory的getExtension方法获取目标对象.ExtensionFactory实现有两个,一个基于SPI的,一个Spring的ApplicationContext的. ...
- LightOJ - 1102 - Problem Makes Problem(组合数)
链接: https://vjudge.net/problem/LightOJ-1102 题意: As I am fond of making easier problems, I discovered ...
- z-tree的使用bug
最近折腾了下z-tree,这个存在一个bug: 新增icon出不来,废话少说上代码: <style type="text/css"> .ztree li span.bu ...
- OSS先后上传相同地址的图片
如果上传先后两张图片那么后面的图片会替换前面的图片
- Launch4j:An error occurred while starting the application.解决方案
长期使用Processing 2.X进行开发,突然有一天Processing 1.5.1打不开了,报错如下: 按[确定]后窗口消失,但是任务管理器中的“javaw.exe”并没有消失..... 试过各 ...
- 2019暑期金华集训 Day6 杂题选讲
自闭集训 Day6 杂题选讲 CF round 469 E 发现一个数不可能取两次,因为1,1不如1,2. 发现不可能选一个数的正负,因为1,-1不如1,-2. hihoCoder挑战赛29 D 设\ ...
- git 导出远程特定分之
很多时候,git clone 只是 clone 下来了 master 分支,如果想 clone 特定分支.有的时候不知如何是好. 找到了如下的命令,记录一下.以便有需要的同学可以使用. git co ...
- ELK教程2:Kibana的安装
kibana作为ElastciSearch的数据查询展示界面,集成了很多的功能,本文主要讲述如下部署kibana. 安装 安装命令如下: # 下载kibana的npm wget https://art ...
- (6)Go函数和函数式编程
一.Go函数 函数是组织好的.可重复使用的.用于执行指定任务的代码块.本文介绍了Go语言中函数的相关内容. Go语言中支持函数.匿名函数和闭包,并且函数在Go语言中属于"一等公民" ...