PAT Advanced 1155 Heap Paths (30 分)
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 分)的更多相关文章
- PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]
题目 In computer science, a heap is a specialized tree-based data structure that satisfies the heap pr ...
- PAT甲级 1155 Heap Paths (30分) 堆模拟
题意分析: 给出一个1000以内的整数N,以及N个整数,并且这N个数是按照完全二叉树的层序遍历输出的序列,输出所有的整条的先序遍历的序列(根 右 左),以及判断整棵树是否是符合堆排序的规则(判断是大顶 ...
- 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, ...
- PAT Advanced 1022 Digital Library (30 分)
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
- PTA 1155 Heap Paths (DFS)
题目链接:1155 Heap Paths (30 分) In computer science, a heap is a specialized tree-based data structure t ...
- PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)
1080 Graduate Admission (30 分) It is said that in 2011, there are about 100 graduate schools ready ...
- 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 ...
- PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to co ...
随机推荐
- 简单实现react中虚拟DOM渲染
/** * @method createElement * @param type {string} * @param props {Object} * @param children {string ...
- SpringBean的工作原理
在 Spring 中,那些组成应用程序的主体及由 Spring IOC 容器所管理的对象,被称之为 bean.简单地讲,bean 就是由 IOC 容器初始化.装配及管理的对象,除此之外,bean 就与 ...
- 使用apache-commons-lang3架构对HTML内容进行编码和反编码
String a="<br>"; String a_str=StringEscapeUtils.escapeHtml4(a);//编码 System.out.print ...
- 【gcd】辗转相除法
#include<stdio.h> int gcd(int a, int b) { int c; while(b) { c = a % b; a = b; b = c; } return ...
- 如何克服社交恐惧症?zz
zhang Bavol 清华大学核能与新能源技术研究院——核科学与技术/电子爱好者 你说的这个恐惧症我也有一点点,不过现在我是只对那种不是很熟悉的七姑八婆之类的亲戚才会有这种憋屈感觉,对朋友和 ...
- [转帖].NET导出Excel的四种方法及评测
.NET导出Excel的四种方法及评测 https://www.cnblogs.com/sdflysha/p/20190824-dotnet-excel-compare.html 导出Excel是.N ...
- 关于日志slf4j+logback&logback.xml配置
1.maven依赖 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <!-- <dependen ...
- 剑指offer14:输入一个链表,输出该链表中倒数第k个结点。
1. 题目描述 输入一个链表,输出该链表中倒数第k个结点. 2. 思路和方法 可以用两个指针,一个指针遍历到第k个结点的时候,第二个指针再走到第一个节点,然后两个指针的距离始终保持k-1.这样,当第一 ...
- python学习-4 python基础-2 条件语句(if的简单用法1)
条件语句的原理: 2.举个例子:比大小 #!/usr/bin/env python # -*- coding:utf8 -*- a=input("请输入a:") b=input(& ...
- Vue路由传参及传参后刷新导致参数消失处理
项目功能需要,要从列表页跳转到第三方提供的URL上(这里第三方页面我是通过iframe引入在详情页,目的是点击返回时可以通过keepAlive让列表页不刷新,如果不通过iframe直接跳第三方链接,那 ...