当前位置:网站首页>Traverse Heap PAT Class A 1155 Heap Path
Traverse Heap PAT Class A 1155 Heap Path
2022-08-02 17:04:00 【keyboard sonata】
In computer science, a heap is a specialized tree-based data structure with heap properties:
If P is the parent node of C, the weight of the P node in the large top heap is greater than or equal to the weight of the C node, and the weight of the P node in the small top heap is less than or equal to the C nodepoint weight.
A common implementation of a heap is a binary heap, which is implemented by a complete binary tree.
To be sure, in a big-top/small-top heap, any path from root to leaf must be in non-increasing/non-decreasing order.
Your task is to check every path in a given complete binary tree to see if it is a heap.
Input format
The first line contains the integer N, which represents the number of nodes in the tree.The second line contains N distinct integers representing the sequence of level-order traversal of the given complete binary tree.
Output format
For a given tree, first output all paths from root to leaf.Each path occupies one line, the numbers are separated by spaces, and there must be no extra spaces at the beginning and end of the line.
The paths must be output in the following order: For each node in the tree, the path in its right subtree must be output before the path in its left subtree.
The last line, if it is a large top heap, output Max Heap, if it is a small top heap, output Min Heap, if it is not a heap, output Not Heap.
Data Range
1Input Example 1:
8
98 72 86 60 65 12 23 50
Example output 1:
98 86 23
98 86 12
98 72 65
98 72 60 50
Max Heap
Input Sample 2:
8
8 38 25 58 52 82 70 60
Output Sample 2:
8 25 70
825 82
8 38 52
8 38 58 60
Min Heap
Input Example 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
My solution:
#include using namespace std;const int N = 1010;int n;int a[N];bool lt, gt;vector path;void dfs(int u){path.push_back(a[u]);if(2 * u > n){cout << path[0];for(int i = 1; i < path. size(); i ++ ) {cout << ' ' << path[i];if(path[i] < path[i - 1]) gt = true;else if(path[i] > path[i - 1]) lt = true;}cout << endl;}if(2 * u + 1 <= n) dfs(2 * u + 1);if(2 * u <= n) dfs(2 * u);path.pop_back();}int main(){cin >> n;for(int i = 1; i <= n; i ++ ){cin >> a[i];}dfs(1);if(lt && gt) puts("Not Heap");else if(lt) puts("Min Heap");else puts("Max Heap");return 0;}
边栏推荐
猜你喜欢
【无标题】
如何正确且快速的清楚C盘!!释放C盘空间内存保姆级教程
有效的括号【暴力、分支判断、哈希表】
VsCode更新后,怎么使用使用快捷键同时生成多个元素
Redis的5中数据类型总结
【 Leetcode string, the string transform/hexadecimal conversion 】 HJ1. The length of the string last word HJ2. Calculation of a certain number of characters appear HJ30. String merging processing
2022-07-26 第六小组 瞒春 学习笔记
公司最大的内卷,是“管理错位”
lammps学习(一)单晶硅纳米磨削
2022-07-25 第六小组 瞒春 学习笔记
随机推荐
Wigner-Ville distribution for time-frequency analysis
How to check the WeChat applet server domain name and modify it
《数字经济全景白皮书》银行业智能风控科技应用专题分析 发布
Redis最新6.27安装配置笔记及安装和常用命令快速上手复习指南
为什么float4个字节比long8个字节所表示的数值范围广
Servlet运行原理_API详解_请求响应构造进阶之路(Servlet_2)
DOM - Event Mechanism and Event Chain
2022-02-14 第五小组 瞒春 学习笔记
H5中的拖放(Drag 和 Drop)
Reading is the cheapest and noblest
2022-07-19 第五小组 瞒春 学习笔记
基于ip的证书
李开复花上千万投的缝纫机器人,团队出自大疆
散列表简述
学习编程的目标
【无标题】
为什么四个字节的float表示的范围比八个字节的long要广?
第六章-6.1-堆-6.2-维护堆的性质-6.3-建堆
【 Leetcode string, the string transform/hexadecimal conversion 】 HJ1. The length of the string last word HJ2. Calculation of a certain number of characters appear HJ30. String merging processing
只出现一次的数字||| —— 哈希映射、异或位运算+分治思想