【PAT - 甲级1155】Heap Paths (30分)(栈,dfs,二叉树)
题干:
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 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
题目大意:
按照标号给定一棵二叉树,问你这是不是个堆,如果是,则是个大根堆还是小根堆。并让你输出所有的从根节点到叶子节点的路径。
解题报告:
注意因为是二叉树所以不需要建树,用一个栈按照要求存储路径即可。最后按照路径进行判断是不是堆,是大根堆还是小根堆。
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
int n;
int val[MAX];
vector<int> stk;
vector<int> ans;
void dfs(int idx) {if(idx * 2 > n) {int up = stk.size(),flag = 1;for(int i = 0; i<up; i++) {printf("%d%c",stk[i],i == up-1 ? '\n' : ' ');}//MAX 5 4 3 2 1for(int i = 1; i<up; i++) {if(stk[i] > stk[i-1]) flag = 0;}if(flag == 1) {ans.push_back(1); return;}//MIN 1 2 3 4 5flag = 1;for(int i = 1; i<up; i++) {if(stk[i] < stk[i-1]) flag = 0;}if(flag == 1) {ans.push_back(2); return;}ans.push_back(-1);return;}if(idx * 2 + 1 <= n) {stk.push_back(val[idx*2+1]);dfs(idx*2+1);stk.pop_back();}if(idx * 2 <= n) {stk.push_back(val[idx*2]);dfs(idx*2);stk.pop_back();}
}
int main()
{cin>>n;for(int i = 1; i<=n; i++) scanf("%d",val+i);stk.push_back(val[1]);dfs(1);int Max = 1,Min = 1;for(auto x : ans) {if(x != 1) Max = 0;}for(auto x : ans) {if(x != 2) Min = 0;} if(Max == 1) printf("Max Heap\n");else if(Min == 1) printf("Min Heap\n");else printf("Not Heap\n");return 0 ;
}
【PAT - 甲级1155】Heap Paths (30分)(栈,dfs,二叉树)相关推荐
- PAT甲级1155 Heap Paths (30 分):[C++题解]堆、堆的遍历、树的遍历、dfs输出路径、完全二叉树建树
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 堆首先是完全二叉树,所以先建完全二叉树,由于给定的是层序遍历的数据,所以直接用数组即可,注意数组下标从1开始,这样便满足结点u和左儿 ...
- 1155 Heap Paths (30 分)【难度: 一般 / 知识点: 堆 堆的遍历】
https://pintia.cn/problem-sets/994805342720868352/problems/1071785408849047552 #include<bits/stdc ...
- PAT甲级1131 Subway Map (30分):[C++题解]堆优化dijkstra、单源最短路、地铁地图、巧妙地建图套dijkstra模板!!
文章目录 题目分析 题目链接 题目分析 原题: 来源:acwing 分析: 建图:所有能走到的点之间建立一条边,比如下面一条地铁线路有4站,它们是相通的,两两之间建一条边,边权是经过的站点数. 下面考 ...
- PAT甲级1072 Gas Station (30 分):[C++题解]dijkstra算法、最短路
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 所有的dist[ ]都≤Ds:最小的dist[ ]最大; dist[ ] 总和最大. 由于加油站是字符,为了简单起见,将m个加油站编 ...
- PAT甲级1107 Social Clusters (30 分):[C++题解]并查集,爱好、人数
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 凭爱好,分人群.注意点:爱好可传递.什么意思?意思是A和B的有共同爱好1, B和C有共同爱好2,那么认为A和C也是同一群人. 按照爱 ...
- PAT甲级1103 Integer Factorization (30 分):[C++题解]背包问题,DP解法
文章目录 题目分析 题目链接 题目分析 分析 把N(样例中N=169)看成背包的体积:把k(样例中k=5)看成背包能承的重量.把这道题转化为二维完全背包问题.由于数据范围给出的次幂P∈[2,7],那么 ...
- PAT甲级1049 Counting Ones (30 分):[C++题解]统计1的个数、数位统计
文章目录 题目分析 题目链接 题目分析 来源:PAT网站 分析: 以数字abcdefg这个7位数字为例,说一下本题的思路. 1)数字1在每一位出现的次数. 2)以第d位为例,第d位的取值可以分为3种情 ...
- PAT甲级1139 First Contact (30 分):[C++题解] 图论、暴力枚举两个点、hash映射
文章目录 题目分析 题目链接 题目分析 来源:acwing 题目分析: 图论模拟题. 给定暗恋的两个人A 和B,需要寻找一对C 和D ,满足:A和C是朋友,C和D是朋友,D和B是朋友.而且A.C同性别 ...
- PAT甲级1111 Online Map (30分):[C++题解]两次dijkstra求单源最短路、保存路径、长度最短、时间最短
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:dijkstra求单源最短路的题目. 只是写两遍而已,第一遍求按照路径长度求,第二遍按照时间最少求. 另外加一个vector路径的判断 ...
- PAT甲级1030 Travel Plan (30分):[C++题解]dijkstra求单源最短路、保存路径
文章目录 题目分析 题目链接 题目分析 来源:PAT网站 分析 dijkstra模板默写过来,然后多了一个保存路径,使用数组pre[N]记录最短路上每个点的前驱,通过pre数组保存到vector中 v ...
最新文章
- 测序仪的序列:DNA测序的历史
- python简单代码input-Python简单程序的练习
- BugKu:cookies 欺骗
- Vue中怎样引入Element
- [转载]如何用关键字优化网站?
- Master DNS服务的搭建
- vue之watch用法
- Docker卸载镜像
- bootlogo画面制作和替换
- 项目微管理23 - 会议
- 仲裁时,年假有效期两年
- 解锁设备_苹果新专利:用户可通过头显设备快速解锁多个附近设备
- 见山只是山 见水只是水——提升对继承的认识
- “天涯博客”“江西福利彩票网”等网站被挂马
- 计算机应用基础客观答案,20春国家开放大学计算机应用基础客观题资料参考答案...
- zblog php getlist,zblog使用getlist方法调用置顶文章
- 【音视频基础】音频基础理论
- SpringCloud微服务注册和消费模式总结
- 技术管理进阶——总监以上一定要会经济学
- JavaScript笔记(一)
热门文章
- 第三课 SVM(2)
- [密码学基础][每个信息安全博士生应该知道的52件事][Bristol52]50.什么是BLS基于对的签名方案?
- [剑指offer][JAVA]面试题[51][数组中的逆序对][归并排序]
- [Leedcode][JAVA][第11题][盛最多水的容器][双指针][贪心]
- CodeForces - 766C - Mahmoud and a Message dp
- ant man 什么意思_浅谈为什么很多蓝牙模块厂家选择nRF52832?
- gtest测试框架使用详解_测试框架TestNG使用介绍
- qstring去掉特定字符_如何花式、批量且操作简单地处理字符?
- mysql命令_MySQL常用操作命令
- php全局cors,PHP开启CORS - slagga的个人页面 - OSCHINA - 中文开源技术交流社区