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 件物品 ...
随机推荐
- CSP-J/S2019试题选做
S D1T2 括号树 设\(f[u]\)表示根到\(u\)的路径上有多少子串是合法括号串.(即题目里的\(k_u\),此变量名缺乏个性,故换之) 从根向每个节点dfs,容易求出\(c[u]\):表示从 ...
- C++ 类 与 static
背景 从学习C++到使用现在,发现很多新的东西,正好整理一下. static 为静态,指是当类编译加载的时候,内存就会开辟存储空间的. static 数据成员 在类中,static 可修饰 类中的成员 ...
- 2-10 就业课(2.0)-oozie:10、伪分布式环境转换为HA集群环境
hadoop 的基础环境增强 HA模式 HA是为了保证我们的业务 系统 7 *24 的连续的高可用提出来的一种解决办法,现在hadoop当中的主节点,namenode以及resourceManager ...
- 第1节 IMPALA:6、yum源制作过程
impala的安装:第一步:下载5个G的安装包,并且上传linux,解压第二步:安装httpd的服务,并启动,访问httpd就是访问我们linux的 /var/www/html这个路径下面的东西第三步 ...
- 如何修改 app.config 的配置信息
如何修改 app.config 的配置信息 收藏 最问这个问题的人有点多,其实 .Net 提供了这样的功能我们可以在 app.config 中 userSettings 节点中保存我们的应用程序设置信 ...
- ReadAsm2
首先查看题目 下载文档之后用虚拟机打开(我用的是Kali Linux) 推测应该是对这个func函数反汇编结果应该就出来了 用c写一下算出结果 #include<bits/stdc++.h> ...
- 洛谷 P2031 脑力达人之分割字串
题目传送门 解题思路: f[i]表示到第i位可获得的最大分割次数,对于每个f[i]都可由其符合条件的前缀转移过来,条件就是当前串除了前缀的剩余字符里有所给单词,然后一看,这不是在剩余字符里找有没有所给 ...
- 牛客周赛11TG B-弹钢琴
链接:https://ac.nowcoder.com/acm/contest/941/B来源:牛客网 题目描述 春希想听和纱弹钢琴! 为了阻止异变的发生,Pi将钢琴魔改了 钢琴上有 N 个键,每个键有 ...
- RAM和ROM的区别
区别如下: 1.概念 RAM(random access memory)即随机存储内存,这种存储器在断电时将丢失其存储内容,故主要用于存储短时间使用的程序.ROM(Read-Only Memory)即 ...
- python3.7的一些心得,不定期更新。
学习的python3.7.2,最新目前是3.8.1 这里记一下主要的几点: pip 是python的模块管理器,姑且这么叫它.和nodejs的npm一样的功能 官网下载python安装包,默认就会按照 ...