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 Heap
if it is a max heap, orMin Heap
for a min heap, orNot 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
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 ...
随机推荐
- php 数组 array()
定义和用法 array() 创建数组,带有键和值.如果在创建数组时省略了键,则生成一个整数键,默认从 0 开始,然后以 1 进行递增. 用 array() 创建一个数组,可使用 => 来分隔键和 ...
- js获取单独一个checkbox是否被选中
<script language=javascript> function check(){ var xz=document.getElementById("xz"); ...
- luogu1357 花园 状态压缩 矩阵快速幂
题目大意 小L有一座环形花园,沿花园的顺时针方向,他把各个花圃编号为1~N(2<=N<=10^15).他的环形花园每天都会换一个新花样,但他的花园都不外乎一个规则,任意相邻M(2<= ...
- luogu3469 [POI2008]BLO_Blockade
题目大意 给一个无向连通图,求对于每一个点,去掉该点后图中连通结点有序对的减少量. 思路 当时想这道题时,我想到:枚举每一个点,在删去它后连通的几个部分中Dfs得到各个部分的点的个数从而得到解,但是我 ...
- STL 集合部分操作
3.28更新 在EOJ 1641 集合栈计算机中,使用并集和补集时候,第五个参数使用x.begin()会报错:assignment of read-only location,而使用inserter( ...
- [Apple开发者帐户帮助]三、创建证书(1)证书概述
在开发应用程序的过程中,您将创建不同的证书类型,以便在不同的上下文中使用.您将为iOS,tvOS和watchOS应用程序使用相同的证书集,并为macOS应用程序使用不同的证书集.您将使用开发证书在设备 ...
- layui富文本编译器添加图片
1.创建富文本编辑器 <form class="layui-form" method="post" id="myForm" encty ...
- EasyUI TreeGrid 悬浮效果
/* 鼠标悬停扩展*/ (function (hasgrid) { if (hasgrid) { /** * 表格提示 */ $.extend($.fn.datagrid.methods, { too ...
- jQuery考试之错题分析
获取元素范围大小顺序依次为: $(#one).siblings("div")>$("#one~div")>$("#one +div&quo ...
- net .异步委托知识
以前在编程中,异步用的比较少,导致C# 一些基础的 东西用法都不怎么熟悉,经常要用的时候在去查找资料比较被动,而已没真正里面理解起来,始终感觉不是自己的知识 (题外话) 首先委托关键字 Delega ...