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), 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 left 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
#include <iostream>
#include <vector>
using namespace std;
int n,heap[];bool isMin=,isMax=;
vector<int> v;
/**思路:先序遍历的镜像表示直接打印*/
void dfs(int index){
if(index*>n && index*+>n){
if(index<=n)
for(int i=;i<v.size();i++){
printf("%d%s",v[i],i!=v.size()-?" ":"\n");
}
}else{
v.push_back(heap[index*+]);
dfs(index*+);
v.pop_back();
v.push_back(heap[index*]);
dfs(index*);
v.pop_back();
}
}
int main()
{
cin>>n;
for(int i=;i<=n;i++) cin>>heap[i];
v.push_back(heap[]);
dfs();
/**判断父亲是否都比孩子大或者小*/
for(int i=;i<=n;i++){
if(heap[i/]>heap[i]) isMin=;
if(heap[i/]<heap[i]) isMax=;
}
if(isMin==) printf("Min Heap");
else if(isMax==) printf("Max Heap");
else printf("Not Heap");
system("pause");
return ;
}
												

PAT Advanced 1155 Heap Paths (30 分)的更多相关文章

  1. PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]

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

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

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

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

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

  4. PAT 甲级 1155 Heap Paths

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

  5. PAT Advanced 1022 Digital Library (30 分)

    A Digital Library contains millions of books, stored according to their titles, authors, key words o ...

  6. PTA 1155 Heap Paths (DFS)

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

  7. PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)

    1080 Graduate Admission (30 分)   It is said that in 2011, there are about 100 graduate schools ready ...

  8. PAT 甲级 1072 Gas Station (30 分)(dijstra)

    1072 Gas Station (30 分)   A gas station has to be built at such a location that the minimum distance ...

  9. PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

    1049 Counting Ones (30 分)   The task is simple: given any positive integer N, you are supposed to co ...

随机推荐

  1. onethink多图上传

    模板处理, 可以参考 checkbox 的. 注:edit 方法的 基本一样,需要先把已有的数据展示出来,绑定双击事件,删除图片 需要先绑定给已经展示出来的图片, uploadPicture 的cal ...

  2. cocos2dx图片加密解密(npk方式)

    话不多说,直接开始: 准备的工具: 链接:https://pan.baidu.com/s/1Om4kBNWcG2jL_RTsHqqzpQ 提取码:bv7i npkCreate.exe是加密图片的工具, ...

  3. 神经网络与数字货币量化交易系列(1)——LSTM预测比特币价格

    首发地址:https://www.fmz.com/digest-topic/4035 1.简单介绍 深度神经网络这些年越来越热门,在很多领域解决了过去无法解决的难题,体现了强大的能力.在时间序列的预测 ...

  4. HIVE的UDF

    HIVE的UDF    新建java工程,导入hive相关包,导入hive相关的lib.    创建类继承UDF    自己编写一个evaluate方法,返回值和参数任意.    为了能让mapred ...

  5. redis的安装和连接

  6. JavaWeb开发常用的前端控件

    罗列的下述控件大多依赖jquery插件,故可提前导入jquery插件以免出错 Validform 提供对表单的验证.提交等功能,具体可查阅其官方文档>>>Validform 示例如下 ...

  7. bootstrap-table的一些配置参数例子

    $('#reportTable').bootstrapTable({ method: 'post', url: '/qStock/AjaxPage', dataType: "json&quo ...

  8. go 二进制数据处理

    以下是利用标准库binary来进行编解码 编码 ①使用bytes.Buffer来存储编码生成的串②使用binary.Write来编码存储在①的buf中 package main import ( &q ...

  9. 解决Eclipse中文文档注释错位-处女座的悲哀!

    1.右键打开eclips属性 2.选择兼容性为win8,然后打开Eclipse即可解决 作者:醉烟 出处:https://www.cnblogs.com/WangLei2018/    本文版权归作者 ...

  10. IT 界的“名言”

    --喜欢记得关注我哟[shoshana]-- 中国有很多古代警世名言,朗朗上口,凝聚了很多故事与哲理.硅谷的互联网公司里头也有一些这样的名言,凝聚了很多公司价值观和做事的方法,对于很多程序员来说,其影 ...