题目

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

题目分析

已知完全二叉树层序序列,打印所有路径(从根节点到叶子节点)并判断是否为堆,为大顶堆还是小顶堆

解题思路

  1. 打印路径

    思路01:dfs深度优先遍历,用整型数组path[n]记录路径进行回溯

    思路02:dfs深度优先遍历,用vector vin链表记录路径进行回溯
  2. 判断是否为堆,为大顶堆还是小顶堆

    思路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, 深搜回溯,堆]的更多相关文章

  1. PAT Advanced 1155 Heap Paths (30 分)

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  2. PAT甲级 1155 Heap Paths (30分) 堆模拟

    题意分析: 给出一个1000以内的整数N,以及N个整数,并且这N个数是按照完全二叉树的层序遍历输出的序列,输出所有的整条的先序遍历的序列(根 右 左),以及判断整棵树是否是符合堆排序的规则(判断是大顶 ...

  3. UVA 165 Stamps (DFS深搜回溯)

     Stamps  The government of Nova Mareterrania requires that various legal documents have stamps attac ...

  4. pat甲级 1155 Heap Paths (30 分)

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  5. PAT 甲级 1155 Heap Paths

    https://pintia.cn/problem-sets/994805342720868352/problems/1071785408849047552 In computer science, ...

  6. PTA 1155 Heap Paths (DFS)

    题目链接:1155 Heap Paths (30 分) In computer science, a heap is a specialized tree-based data structure t ...

  7. HDU5723 Abandoned country (最小生成树+深搜回溯法)

    Description An abandoned country has n(n≤100000) villages which are numbered from 1 to n. Since aban ...

  8. DFS深搜——Red and Black——A Knight&#39;s Journey

    深搜,从一点向各处搜找到全部能走的地方. Problem Description There is a rectangular room, covered with square tiles. Eac ...

  9. DFS 深搜专题 入门典例 -- 凌宸1642

    DFS 深搜专题 入门典例 -- 凌宸1642 深度优先搜索 是一种 枚举所有完整路径以遍历所有情况的搜索方法 ,使用 递归 可以很好的实现 深度优先搜索. 1 最大价值 题目描述 ​ 有 n 件物品 ...

随机推荐

  1. (转)浅谈 Linux 内核无线子系统

    前言 Linux 内核是如何实现无线网络接口呢?数据包是通过怎样的方式被发送和接收呢? 刚开始工作接触 Linux 无线网络时,我曾迷失在浩瀚的基础代码中,寻找具有介绍性的材料来回答如上面提到的那些高 ...

  2. 014.CI4框架CodeIgniter数据库操作之:查询数据库,并让数据以对象的方式返回查询结果

    01. 我们在CI4框架中的Model文件夹新建一个User_model.php的文件,使用的是getResultArray,表示并让数据以数组的方式返回查询结果,代码如下: <?php nam ...

  3. 2.11 学习总结 之 ajax

    一.说在前面 昨天 学习了 json 数据结构 今天 学习ajax 并使用 json  二.jquery的ajax操作 1.查询jquery的官方文档发现与ajax相关的jquey方法如下: 1)$. ...

  4. HDU - 6143 Killer Names(dp记忆化搜索+组合数)

    题意:从m种字母中选取字母组成姓名,要求姓和名中不能有相同的字母,姓和名的长度都为n,问能组成几种不同的姓名. 分析: 1.从m种字母中选取i种组成姓,剩下m-i种组成名. 2.i种字母组成长度为n的 ...

  5. POJ 2155:Matrix 二维树状数组

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21757   Accepted: 8141 Descripti ...

  6. Rails 第一课:环境配置 Ruby Rails RVM Heroku

    安装 上传专案到 Heroku 安装 Ruby 2.3.1 Rails 5.0.0.1 RVM 1.27.0 比较幸运一个问题都未碰到 MacOS 10.12.1 配置完成系统顺利升级到10.12.2 ...

  7. jenkins打包iOS 报错:error: exportArchive: The data couldn’t be read because it isn’t in the correct format.

    在执行ios 打包的时候,我们通过执行下面的指令来打包ipa: mkdir arch archive_path=arch/${app_name}.xcarchive workspace_name=HP ...

  8. error LNK2019: 无法解析的外部符号……

    在VS中开发程序的时候遇到一个问题,应该算是比较常见,所以记录下. 在编译程序的时候遇到一个错误,大致提示如下: "error LNK2019: 无法解析的外部符号--" 遇到这个 ...

  9. Python 中 configparser 配置文件的读写及封装,配置文件存放数据,方便修改

    1. 将程序中不常变化的数据放在配置文件中,有什么好处? 将配置统一放在一起,进行统一管理,方便维护,方便修改 配置文件将存放测试数据比如: Excel文件名. 日志名. 用例执行的结果. 实际结果和 ...

  10. 工作问题整理-- sqlserver 新增参数限制,maven pom邮件发送

    1.SqlServer连续新增参数限制 com.microsoft.sqlserver.jdbc.SQLServerException: 传入的请求具有过多的参数.该服务器支持最多 2100 个参数. ...