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 ...
随机推荐
- MySQL问题:Access denied for user 'mysql'@'localhost'
deep@deep-PC:~$ sudo mysql -uroot -p mysql> update mysql.user set authentication_string=PASSWORD( ...
- node.js 安装配置笔记
先设置 NODEJS_MODULES 系统环境变量,我在这里设置为:D:\Program Files\nodejs\node_modules, 然后修改 npm.cmd 文件中 SET "N ...
- sed工具的基本用法
sed文本处理工具的用法: 用法1:前置命令 | sed [选项] '条件指令' 用法2:sed [选项] '条件指令' 文件.. .. 认识sed工具的基本选项 sed命令的常用选项如下: -n(屏 ...
- 【ARM-Linux开发】Linux下查看机器的CPU负载
负载(load)是Linux机器的一个重要指标,直观了反应了机器当前的状态.如果机器负载过高,那么对机器的操作将难以进行. Linux的负载高,主要是由于CPU使用.内存使用.IO消耗三部分构成.任意 ...
- 【VS开发】文件共享内存2
在32位的Windows系统中,每一个进程都有权访问他自己的4GB(232=4294967296)平面地址空间,没有段,没有选择符,没有near和far指针,没有near和far函数调用,也没有内存模 ...
- vue兄弟组件和v-slot之间的关系
如果子组件 没有包含一个 < slot > 元素,则任何传入它的内容都会被抛弃 ,因此我们引入了插槽,可以添加任意内容进子组件.
- Reactor系列(八)concatMap有序映射
#java#reactor#comcatMap# 有序映射 视频讲解:https://www.bilibili.com/video/av79705356/ FluxMonoTestCase.java ...
- luogu P1216 [IOI1994][USACO1.5]数字三角形 Number Triangles (递推)
链接:https://www.luogu.org/problemnew/show/P1216 题面: 题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的 ...
- java微信token校验
1.微信验证接口 package com.park.utils.wechatUtil; import org.springframework.web.bind.annotation.RequestMa ...
- 【转载】java8 自定义TemporalAdjuster
有的时候,你需要进行一些更加复杂的操作,比如,将日期调整到下个周日.下个工作日,或者是本月的最后一天.这时,你可以使用重载版本的with方法,向其传递一个提供了更多定制化选择的TemporalAdju ...