题目

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. tools.quartz.about

    官方网站,中文文档,demo,  参考零, 参考一, 参考二, 参考三, 参考四 , 参考五 ,文档下载 .

  2. uboot源码分析1-启动第一阶段

    1.不简单的头文件包含 #include <config.h>:这个文件的内容其实是包含了一个头文件:#include <configs/x210_sd.h>". # ...

  3. 001.CI4框架CodeIgniter的默认访问路径url

    1. 我们解压缩CI4的压缩包,找到app目录,点开Controllers目录,在Home.php文件中,写入我们的如下代码: 002.我们来访问我们的网站 http://127.0.0.1/CI4/ ...

  4. Eclipse 不能调试的问题

    现象 弹出 Cannot connect to VM Console 中的输出是: ERROR: transport error 202: connect failed: Connection ref ...

  5. asp.net+sql数据库学生信息管理系统

    一款学生信息管理系统送给大家. 功能部分: 1.教师管理(添加,修改,删除,查看) 2.学生管理(添加,修改,删除,查看) 3.班级管理(添加,修改,删除,查看) 4.学生成绩管理(添加,修改,删除, ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-download

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

  7. (四)Flex 布局教程和例子

    布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现. 1.flex-direction ...

  8. c++程序—选择结构

    if(判断条件){执行语句} #include<iostream> using namespace std; #include<string> int main() { ; c ...

  9. ubuntu 中加速pip指令下载插件的速度

    在使用pip下载时很多时候下载速度特别慢,时不时就会发生timeout. 这是因为安装源与本机之间网络不畅导致,其实可以自己指定pip的下载来源,就像指定ubuntu更新源那样. 接下来谈谈步骤: 1 ...

  10. POJ1611 && POJ2524 并查集入门

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 28293   Accepted: 13787 De ...