PAT-1064 Complete Binary Search Tree(完全二叉树)
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
题目大意:给出一个构成完全二叉树(除了最后一层的右边可能缺少部分节点,其余层均达到最大节点数)的序列,要求输出该完全二叉树的层次遍历。
主要思想:根据完全二叉树的特性可以用一个数组很方便的表示出来,将根节点的索引设为1,之后每个索引为i的节点,其左节点为2i,右节点为2i+1,然后对于输入的序列进行排序后按照中序遍历依次填入二叉树数组,这样完全二叉树就构造成功了,而最后一步的层次遍历输出其实就是对数组的顺序输出。
#include <cstdio>
#include <algorithm>
int n; //节点个数
int index = 0; //序列数组的索引
int a[1005]; //输入的序列数组
int node[1005]; //完全二叉树数组
using namespace std;
void travel(int i) {
if (i > n) return;
travel(2*i);
node[i] = a[index++];
travel(2*i+1);
}
int main(void) {
int i;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a, a+n);
travel(1); //中序遍历构造完全二叉树
//层次遍历输出
for (i = 1; i < n; i++)
printf("%d ", node[i]);
printf("%d\n", node[i]);
return 0;
}
PAT-1064 Complete Binary Search Tree(完全二叉树)的更多相关文章
- PAT 1064 Complete Binary Search Tree[二叉树][难]
1064 Complete Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a b ...
- PAT 1064 Complete Binary Search Tree
#include <iostream> #include <cstdio> #include <cstdlib> #include <vector> # ...
- 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 ...
- 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 ...
- 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 甲级 1064 ( Complete Binary Search Tree ) (数据结构)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binar ...
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...
随机推荐
- java 设计模式-责任链
责任链设计模式,其实就是处理同一个请求的对象连接成一条链,请求的路径经过这条链,符合要求的就处理这个请求,不符合就接着往下面抛出,直道有人处理这条请求. 业务:比如啊,公司个人请假,三天以下就是主管审 ...
- Algs4-2.1.17动画-选择排序
2.1.17动画.修改插入排序和选择排序的代码,使之将数组内容绘制成正文中所示的棒状图.在每一轮排序后重绘图片来产生动画效果,并以一张"有序"的图片作为结束,即所有圆棒均已按照高度 ...
- Linux 下如何产生core文件(core dump设置)
转自:https://blog.csdn.net/star_xiong/article/details/43529637 今天在Linux下调试C程序时,出现段错误,习惯性的ls下当前目录,发现没有生 ...
- Eating Everything Efficiently(反向dp)
传送门 取最大值即可.用拓扑,dfs都可以实现 #include <bits/stdc++.h> using namespace std; const int maxn=500009; i ...
- spring的后台数据校验
数据校验对于开发项目来说是必须的.校验一般分为前台校验和后台校验,前台校验是必须要做的,后台校验是可选的.后台校验相对前台校验来说配置起来一般更复杂.前台校验通过js做,前台校验一般非常容易绕过.sp ...
- 随笔 - B树算法实现
写代码之前,再回顾一下B树是什么,满足什么样的规则 B树规则: 排序方式:所有节点关键字是按递增次序排列,并遵循左小右大原则 子节点数:非叶节点的子节点数>1,且<=M ,且M>=2 ...
- ssh暴力破解解决方案(Centos7更改端口)
服务器默认ssh远程连接端口为22端口,通常通过22远程连接的话,容易有ssh暴力破解的风险,给我们造成一定的损失.下面是更换ssh端口过程: 1.添加ssh端口 vim /etc/ssh/sshd_ ...
- C# 数据操作系列 - 3. ADO.NET 离线查询
0. 前言 在上一篇中,我故意留下了查询的示范没讲.虽然说可以通过以下代码获取一个DataReader: IDataReader reader = command.ExecuteReader(); 然 ...
- Altera特殊管脚的使用
- 【SMB源码解析系列】——003.SMB游戏基本框架
前面有了解到RESET中断相关代码,结尾处通过一句jmp进入了无限循环,之后CPU将会在每一帧PUU进入VBlank状态时,接收NMI中断信号, 跳转至NMI代码处继续执行,直到遇见RTI指令时又返回 ...