PAT_A1155#Heap Paths
Source:
PAT A1155 Heap Paths (30 分)
Description:
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 Heapif it is a max heap, orMin Heapfor a min heap, orNot Heapif 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
Keys:
- 堆(Heap);
- 完全二叉树(Complete Binary Tree);
- 深度优先搜索(Depth First Search);
Attention:
- 完全二叉树采用静态存储,CBTree[1...n],从2/n+1开始为叶子结点;
- 采用RNL遍历
Code:
/*
Data: 2019-08-03 19:57:18
Problem: PAT_A1155#Heap Paths
AC: 33:40 题目大意:
层次遍历给出一棵完全二叉树,
输出根结点到各个叶子结点的路径,并判断是否为堆
输出顺序:从最右叶子到最左叶子
基本思路:
根右左的顺序遍历二叉树并存储路径,到达叶子结点时打印路径
若父亲大于孩子,则非小根堆;
若父亲小于孩子,则非大根堆;
*/
#include<cstdio>
#include<vector>
using namespace std;
const int M=1e3+;
int n,heap[M],Max=,Min=;
vector<int> path; void Travel(int root)
{
if(root > n)
return;
path.push_back(heap[root]);
if(root!= && heap[root]>heap[root/])
Max=;
if(root!= && heap[root]<heap[root/])
Min=;
if(root > n/)
for(int i=; i<path.size(); i++)
printf("%d%c", path[i], i==path.size()-?'\n':' ');
Travel(root*+);
Travel(root*);
path.pop_back();
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d", &n);
for(int i=; i<=n; i++)
scanf("%d", &heap[i]);
Travel();
if(Max) printf("Max Heap");
else if(Min) printf("Min Heap");
else printf("Not Heap"); return ;
}
PAT_A1155#Heap Paths的更多相关文章
- PTA 1155 Heap Paths (DFS)
题目链接:1155 Heap Paths (30 分) In computer science, a heap is a specialized tree-based data structure t ...
- PAT A1155 Heap Paths (30 分)——完全二叉树,层序遍历,特定dfs遍历
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甲级 1155 Heap Paths (30 分)
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- PAT Advanced 1155 Heap Paths (30 分)
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]
题目 In computer science, a heap is a specialized tree-based data structure that satisfies the heap pr ...
- 1155 Heap Paths (30 分)(堆+dfs遍历)
比较简单的一题 遍历左右的时候注意一下 #include<bits/stdc++.h> using namespace std; ; ]; ; vector<int>t; ve ...
- PAT甲级 1155 Heap Paths (30分) 堆模拟
题意分析: 给出一个1000以内的整数N,以及N个整数,并且这N个数是按照完全二叉树的层序遍历输出的序列,输出所有的整条的先序遍历的序列(根 右 左),以及判断整棵树是否是符合堆排序的规则(判断是大顶 ...
- 1155 Heap Paths
题干前半略. 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 ...
随机推荐
- java 的collection
参考:http://skyuck.iteye.com/blog/526358 https://www.tutorialspoint.com/java/java_collections.htm Prio ...
- 《从零開始学Swift》学习笔记(Day60)——Core Foundation框架
创文章,欢迎转载.转载请注明:关东升的博客 Core Foundation框架是苹果公司提供一套概念来源于Foundation框架,编程接口面向C语言风格的API.尽管在Swift中调用这样的C语 ...
- 在CentOS 6 中安装 Apache,Mysql, PHP
1.安装Apache 在终端中输入以下的命令就能够安装Apache了: sudo yum install httpd sudo的意思是用root用户做什么操作.要点击y就确认下载安装了,非常方便. 然 ...
- c++中cin的基本使用方法
一.最主要的使用方法cin>> 接收一个数字.字符.字符串.遇"空格"."TAB"."回车"都结束 比如: <span s ...
- 《CS:APP》 chapter 9 Vitrual Memory 笔记
Vitrual Memory In order to manage memory more efficiently and with fewer errors, modern systems prov ...
- SQL语句改动表名和字段名
今天有个暂时任务,改动生产环境的数据库表名和字段名.曾经要改动表名字段名都是在开发环境.直接打开 SQL Server找到相应的表或字段重命名就OK啦,但是这是线上数据库,再想直接F2改动是不可能的啦 ...
- 线段树 hdu3255 Farming
做了这么多扫描线的题,,基本都是一个思路. 改来改去,,无非就是维护的节点的内容以及push_up越写越复杂了而已 首先将价格排序处理一下编号,变成编号越大的powerfol越大 然后后面加入扫描线的 ...
- [POJ 2536] Gopher ||
[题目链接] http://poj.org/problem?id=2536 [算法] 匈牙利算法解二分图最大匹配 [代码] #include <algorithm> #include &l ...
- Java-java-com-util-common-service:ServiceException.java
ylbtech-Java-java-com-util-common-service:ServiceException.java 1.返回顶部 1. package com.shineyoo.manag ...
- c programs