PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]
题目
In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of C. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree. (Quoted from Wikipedia at https://en.wikipedia.org/wiki/Heap_ (data_structure)) One thing for sure is that all the keys along any path from the root to a leaf in a max/min heap must be in non-increasing/non-decreasing order.
Your job is to check every path in a given complete binary tree, in order to tell if it is a heap or not.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (1<N≤1,000), the number of keys in the tree. Then the next line contains N distinct integer keys (all in the range of int), which gives the level order traversal sequence of a complete binary tree.
Output Specification:
For each given tree, first print all the paths from the root to the leaves. Each path occupies a line, with all the numbers separated by a space, and no extra space at the beginning or the end of the line. The paths must be printed in the following order: for each node in the tree, all the paths in its right subtree must be printed before those in its lef subtree.
Finally print in a line Max Heap if it is a max heap, or Min Heap for a min heap, or Not Heap if it is not a heap at all.
Sample Input 1:
8
98 72 86 60 65 12 23 50
Sample Output 1:
98 86 23
98 86 12
98 72 65
98 72 60 50
Max Heap
Sample Input 2:
8
8 38 25 58 52 82 70 60
Sample Output 2:
8 25 70
8 25 82
8 38 52
8 38 58 60
Min Heap
Sample Input 3:
8
10 28 15 12 34 9 8 56
Sample Output 3:
10 15 8
10 15 9
10 28 34
10 28 12 56
Not Heap
题目分析
已知完全二叉树层序序列,打印所有路径(从根节点到叶子节点)并判断是否为堆,为大顶堆还是小顶堆
解题思路
- 打印路径
思路01:dfs深度优先遍历,用整型数组path[n]记录路径进行回溯
思路02:dfs深度优先遍历,用vector vin链表记录路径进行回溯 - 判断是否为堆,为大顶堆还是小顶堆
思路01:递归判断,用父节点与其左右子节点进行比较判断
思路02:for循环,用所有子节点与其父节点进行比较判断
Code
Code 01
#include <iostream>
using namespace std;
/*
利用数组回溯
*/
int level[1001],path[1001];
int n;
void printPath(int pin) {
for(int i=0; i<=pin; i++) {
printf("%d",path[i]);
printf("%s",i==pin?"\n":" ");
}
}
void dfs(int vin, int pin) {
path[pin]=level[vin];
if(2*vin+1>=n) { //左右子节点都为NULL 2*vin+1>=n则一定2*vin+2>=n
printPath(pin);
return;
} else if(2*vin+2>=n) { //左子节点非NULL 右子节点为NULL
path[pin+1]=level[2*vin+1]; //添加左子节点后 打印退出
printPath(pin+1);
return;
} else {
dfs(2*vin+2,pin+1);
dfs(2*vin+1,pin+1);
}
}
bool isMaxHeap(int vin) {
if(2*vin+1>=n)return true; //左右子节点都为NULL 2*vin+1>=n则一定2*vin+2>=n
if(2*vin+1<n&&level[2*vin+1]>level[vin])return false;
if(2*vin+2<n&&level[2*vin+2]>level[vin])return false;
return isMaxHeap(2*vin+1)&&isMaxHeap(2*vin+2);
}
bool isMinHeap(int vin) {
if(2*vin+1>=n)return true; //左右子节点都为NULL 2*vin+1>=n则一定2*vin+2>=n
if(2*vin+1<n&&level[2*vin+1]<level[vin])return false;
if(2*vin+2<n&&level[2*vin+2]<level[vin])return false;
return isMinHeap(2*vin+1)&&isMinHeap(2*vin+2);
}
int main(int argc,char * argv[]) {
scanf("%d",&n);
for(int i=0; i<n; i++) {
scanf("%d",&level[i]);
}
dfs(0,0);
if(isMaxHeap(0)) {
printf("Max Heap\n");
} else if(isMinHeap(0)) {
printf("Min Heap\n");
} else {
printf("Not Heap\n");
}
return 0;
}
Code 02
#include <iostream>
#include <vector>
using namespace std;
/*
利用链表回溯
*/
int level[1001];
vector<int> path;
int n,isMax=1,isMin=1;
void printPath() {
for(int i=0; i<path.size(); i++) {
printf("%d",path[i]);
printf("%s",i==pin?"\n":" ");
}
}
void dfs(int vin) {
if(2*vin+1>=n) { //左右子节点都为NULL 2*vin+1>=n则一定2*vin+2>=n
printPath();
} else if(2*vin+2>=n) {//左子节点非NULL 右子节点为NULL
path.push_back(level[2*vin+1]);
printPath();
path.pop_back();
} else {
path.push_back(level[2*vin+2]);
dfs(2*vin+2);
path.pop_back();
path.push_back(level[2*vin+1]);
dfs(2*vin+1);
path.pop_back();
}
}
int main(int argc,char * argv[]) {
scanf("%d",&n);
for(int i=0; i<n; i++) {
scanf("%d",&level[i]);
}
path.push_back(level[0]);
dfs(0);
for(int i=1;i<n;i++){
if(level[(i-1)/2]>level[i])isMin=0; //如果i是从1存储的,这里应该是level[i/2]>level[i]
if(level[(i-1)/2]<level[i])isMax=0;
}
if(isMax==1) {
printf("Max Heap\n");
} else if(isMin==1) {
printf("Min Heap\n");
} else {
printf("Not Heap\n");
}
return 0;
}
Code 03
#include <iostream>
using namespace std;
/*
利用数组回溯
*/
int level[1001],path[1001];
int n,isMax=1,isMin=1;
void printPath(int pin) {
for(int i=0; i<=pin; i++) {
printf("%d",path[i]);
printf("%s",i==pin?"\n":" ");
}
}
void dfs(int vin, int pin) {
path[pin]=level[vin];
if(2*vin+1>=n) { //左右子节点都为NULL 2*vin+1>=n则一定2*vin+2>=n
printPath(pin);
return;
} else if(2*vin+2>=n) { //左子节点非NULL 右子节点为NULL
path[pin+1]=level[2*vin+1]; //添加左子节点后 打印退出
printPath(pin+1);
return;
} else {
dfs(2*vin+2,pin+1);
dfs(2*vin+1,pin+1);
}
}
int main(int argc,char * argv[]) {
scanf("%d",&n);
for(int i=0; i<n; i++) {
scanf("%d",&level[i]);
}
dfs(0,0);
for(int i=1;i<n;i++){
if(level[(i-1)/2]>level[i])isMin=0; //如果i是从1存储的,这里应该是level[i/2]>level[i]
if(level[(i-1)/2]<level[i])isMax=0;
}
if(isMax==1) {
printf("Max Heap\n");
} else if(isMin==1) {
printf("Min Heap\n");
} else {
printf("Not Heap\n");
}
return 0;
}
PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]的更多相关文章
- PAT Advanced 1155 Heap Paths (30 分)
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- PAT甲级 1155 Heap Paths (30分) 堆模拟
题意分析: 给出一个1000以内的整数N,以及N个整数,并且这N个数是按照完全二叉树的层序遍历输出的序列,输出所有的整条的先序遍历的序列(根 右 左),以及判断整棵树是否是符合堆排序的规则(判断是大顶 ...
- UVA 165 Stamps (DFS深搜回溯)
Stamps The government of Nova Mareterrania requires that various legal documents have stamps attac ...
- pat甲级 1155 Heap Paths (30 分)
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- PAT 甲级 1155 Heap Paths
https://pintia.cn/problem-sets/994805342720868352/problems/1071785408849047552 In computer science, ...
- PTA 1155 Heap Paths (DFS)
题目链接:1155 Heap Paths (30 分) In computer science, a heap is a specialized tree-based data structure t ...
- HDU5723 Abandoned country (最小生成树+深搜回溯法)
Description An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since aban ...
- DFS深搜——Red and Black——A Knight's Journey
深搜,从一点向各处搜找到全部能走的地方. Problem Description There is a rectangular room, covered with square tiles. Eac ...
- DFS 深搜专题 入门典例 -- 凌宸1642
DFS 深搜专题 入门典例 -- 凌宸1642 深度优先搜索 是一种 枚举所有完整路径以遍历所有情况的搜索方法 ,使用 递归 可以很好的实现 深度优先搜索. 1 最大价值 题目描述 有 n 件物品 ...
随机推荐
- dMd----攻防世界
首先在Linux上查看题目,没有什么发现elf文件,之后使用ida打开看看,找到main函数,f5查看, 上图一些字符是char过的,便于查看,发现是一个if else语句,先经过了MD5加密然后判断 ...
- 【capstone/ropgadget】环境配置
具体环境配置可参考 https://github.com/JonathanSalwan/ROPgadget/tree/master 作者给出的安装方式 但具体配置中出现了问题,如引用时出现如下错误: ...
- 针对phpstudy默认设置的利用
在phpstudy下载下来以后路径,设置没有修改的情况下可以使用此方法 url:http://ip/phpmyadmin用户名:root 密码:root登入管理界面变量-->general lo ...
- 如何创建一个SpringBoot多模块项目
创建主模块rail-plate-line 1.点击Create New Project --> 选择Spring Initializr -- > 选择本地jdk 2.Group为com ...
- C++中的简单继承
Father.cpp: #include<iostream> using namespace std; class Father { protected: int width; int h ...
- bug-解决微信页面input键盘不回弹问题
pageReturn () { this.$refs.phoneValue.blur(); this.$refs.verifyCode.blur(); setTimeout(() => { wi ...
- 08.swoole学习笔记--异步tcp客户端
<?php //创建异步tcp客户端 $client=new swoole_client(SWOOLE_SOCK_TCP,SWOOLE_SOCK_ASYNC); //注册连接成功的回调函数 $c ...
- Run K8s / 安装指南
Windows 下载 kubectl 官方文档下载对应操作系统的 Kubectl 下载 minikube 如图将下载的文件放在一起,如图: 配置环境变量,如图: 配置Hype-V或者安装Vir ...
- java注解——内置注解和四种元注解
java内置注解: @Override(重写方法):被用于标注方法,用于说明所标注的方法是重写父类的方法 @Deprecated(过时方法):用于说明所标注元素,因存在安全问题或有更好选择而不鼓励使用 ...
- 谈谈对MapTask任务分配和Shuffle的理解
一.切片与MapTask的关系 1.概述 大家要注意区分切片与切块的区别: 切块Block是HDFS物理上把数据分成一块一块的,默认是128M: 数据切片:只是在逻辑上对输入进行分片,并不会在磁盘上分 ...