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, 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

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的更多相关文章

  1. PTA 1155 Heap Paths (DFS)

    题目链接:1155 Heap Paths (30 分) In computer science, a heap is a specialized tree-based data structure t ...

  2. PAT A1155 Heap Paths (30 分)——完全二叉树,层序遍历,特定dfs遍历

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  3. PAT 甲级 1155 Heap Paths

    https://pintia.cn/problem-sets/994805342720868352/problems/1071785408849047552 In computer science, ...

  4. pat甲级 1155 Heap Paths (30 分)

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  5. PAT Advanced 1155 Heap Paths (30 分)

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  6. PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]

    题目 In computer science, a heap is a specialized tree-based data structure that satisfies the heap pr ...

  7. 1155 Heap Paths (30 分)(堆+dfs遍历)

    比较简单的一题 遍历左右的时候注意一下 #include<bits/stdc++.h> using namespace std; ; ]; ; vector<int>t; ve ...

  8. PAT甲级 1155 Heap Paths (30分) 堆模拟

    题意分析: 给出一个1000以内的整数N,以及N个整数,并且这N个数是按照完全二叉树的层序遍历输出的序列,输出所有的整条的先序遍历的序列(根 右 左),以及判断整棵树是否是符合堆排序的规则(判断是大顶 ...

  9. 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 ...

随机推荐

  1. PF, Page File, 页面文件

    PF, Page File, 页面文件 是硬盘中用来当作内存使用的,仅仅提高物理内存可能导致CPU使用率高,因为降低了命中率: 学习了:https://baike.baidu.com/item/PF% ...

  2. 前台JSON字符串,spring mvc controller也接收字符串

    前台JSON字符串,spring mvc controller也接收字符串 前台: $.post(url, { data : JSON.stringify(obj) }, function(data) ...

  3. 2.5-冗余VLAN

    2.5-冗余VLAN     High-Availability(AH) First hop routers on the LAN redundancy Network/首跳冗余网络(出口第一个网络) ...

  4. ios单元測试之GHUnit

    1.相同创建一个測试的project, 2.通过cocoaPod来下载GHUnit框架,或者到github上下载.由于这个框架是开源的第三方框架. 同一时候加入QuartCore.framework( ...

  5. ASP原码加密工具介绍

    ASP原码加密工具介绍 总是会有非常多方法暴露ASP的原程序.造成数据库的password 路径都能够轻易被其它人搞到,所以对ASP程序实行加密处理是个不错的解决方法.以下来介绍一个工具假设大家感兴趣 ...

  6. 每日算法之二十六:Substring with Concatenation of All Words

    变相的字符串匹配 给定一个字符串,然后再给定一组同样长度的单词列表,要求在字符串中查找满足下面条件的起始位置: 1)从这个位置開始包括单词列表中全部的单词.且每一个单词仅且必须出现一次. 2)在出现的 ...

  7. oc50--@class1

    // // main.m #import <Foundation/Foundation.h> #import "Person.h" int main(int argc, ...

  8. Android休眠唤醒机制简介(一)【转】

    本文转载自:http://blog.csdn.net/zhaoxiaoqiang10_/article/details/24408129 Android休眠唤醒机制简介(一) ************ ...

  9. 没有终结点在侦听可以接受消息的 http://192.168.1.63:8085/LoginService。这通常是由于不正确的地址或者 SOAP 操作导致的

    2016-04-08 09:15:05,581 [8] ERROR System.Threading.Thread - ErrorSystem.ServiceModel.EndpointNotFoun ...

  10. NYOJ15括号匹配

    NYOJ15括号匹配 括号匹配(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:6   描述 给你一个字符串,里面只包含"(",")" ...