题目

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. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:显示和隐藏内容

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. JuJu团队1月9号工作汇报

    JuJu团队1月9号工作汇报 JuJu   Scrum 团队成员 今日工作 剩余任务 困难 飞飞 将示例程序打包成exe 将crossentrophy和softmax连接起来 无 婷婷 -- 完善ma ...

  3. 你必须知道的.Net 8.2.2 本质分析

    1 .Equals  静态方法  Equals 静态方法实现了对两个对象的相等性判别,其在 System.Object 类型中实现过程可以表 示为: public static bool Equals ...

  4. Easy_Re

    这题比较简单,一波常规的操作之后直接上ida(小白的常规操作在以前的博客里都有所以这里不在赘述了),ida打开之后查看一下, 这里应该就是一个入口点了,接着搜索flag字符串, 上面的黄色的部分转换成 ...

  5. ubuntu16下安装mongodb 3.6

    1.安装MongoDB社区版     # 1. 导入MongoDB public GPG Key sudo apt-key adv --keyserver hkp://keyserver.ubuntu ...

  6. CVE-2019-0708—微软RDP远程桌面代码执行漏洞复现

    0x01 2019年9月7日凌晨,msf上更新了0708的漏洞利用程序. 顿时安全群和朋友圈就爆炸了 - 奈何接到HW攻击队任务,又在家过了个中秋,0708才在今天更新. 0x02 环境 Window ...

  7. PhoneGap简易配置使用

    在Android Studio 里新一下Android项目, 这个不用说了. 链接: https://pan.baidu.com/s/1qYcCBEW 密码: ymhh 添加 cordovaapp-c ...

  8. dmp文件自动分析

    dmp文件的分析,可以借助各种工具,比如WinDbg, CDB , NTSD,KD等.Windbg提供了窗口接口,而CDB , NTSD是基于命令行的工具,它们都使用了同样的调试引擎Dbgeng.dl ...

  9. Python中pandas透视表pivot_table功能详解(非常简单易懂)

    一文看懂pandas的透视表pivot_table 一.概述 1.1 什么是透视表? 透视表是一种可以对数据动态排布并且分类汇总的表格格式.或许大多数人都在Excel使用过数据透视表,也体会到它的强大 ...

  10. Angular2的双向数据绑定

    什么是双向绑定 如图:   双向绑定.jpg 双向绑定机制维护了页面(View)与数据(Data)的一致性.如今,MVVM已经是前段流行框架必不可少的一部分. Angular2中的双向绑定 双向绑定, ...