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 ...
随机推荐
- 新式类__new__()方法
概述 __new__() 是在新式类中新出现的方法,在 Python 中类实例化时,__new__()方法用在 __init__() 启动之前,决定是否要使用该 __init__() 方法,因为__n ...
- c# 子窗体居中父窗体
1.设置CenterParent不管用.只好用代码控制. frmRunning_ = new FrmRunning(); frmRunning_.StartPosition = FormStartPo ...
- JavaScript快速开发
c标签导入 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> fn函数 ...
- Mysql按照设计顺序获得某个表的字段名称,字段类型,字段描述!!!!!
编写sql语句 select column_name,data_type ,column_comment from information_schema.columns where table_nam ...
- tomcat-APR配置及三种工作模式简介
安装软件包,之前可以用rpm -q 命令查看一下是否存在,如果有这两个软件包先卸载再重新安装yum -y install apr apr-devel 拷贝Tomcat安装目录下的bin目录下的tomc ...
- vue8种通信方式
参考:https://juejin.im/post/5d267dcdf265da1b957081a3#heading-1(写的很详细) https://blog.csdn.net/songxiu ...
- Cron 表达式学习
1.7个子域的说明 cron 的表达式是字符串,实际上是由七子表达式(从左到右),描述个别细节的时间表.这些子表达式是分开的空白. 顺序(从左到右) 子串 有效数字 有效字符 1 Seconds(秒) ...
- 【leetcode算法-简单】9. 回文数
[题目描述] 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121输出: true示例 2: 输入: -121输出: false解释: ...
- [bzoj3060][Poi2012]Tour de Byteotia_并查集
[Poi2012]Tour de Byteotia 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3060 题解: 这类题有一个套路,就是 ...
- 适合新手的160个creakme(二)
先跑一下,然后找出关键字符串 关键字符串是You Get Wrong和Try Again,不过IDA好像识别不出来这个字符串,在Ollydbg中右键Search For,寻找所有字符串,可以找到这些字 ...