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. 洛谷 P2023 [AHOI2009]维护序列

    P2023 [AHOI2009]维护序列 题目描述 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中 ...

  2. SpringMVC项目

    一.说明该项目是为了演示SpringMVC框架中涉及到的一些知识点,相对独立,掌握这些,基本上就能够解决工作中遇到的一些问题.整个项目的构建规划如下: 1.创建一个标准的Maven Web项目;2.使 ...

  3. applicationcontext理解使用

    Spring ApplicationContext 容器 Application Context 是 spring 中较高级的容器.和 BeanFactory 类似,它可以加载配置文件中定义的 bea ...

  4. vijos - P1279Leave-绿光(数学归纳法 + python)

    P1279Leave-绿光 Accepted 标签:[显示标签] 背景 期待这一份幸运,和一份冲劲,多么奇异的际遇--. 燕姿在演唱完绿光这首歌后,出给了姿迷一个考题. 北欧有一个传说! 人一生中能看 ...

  5. python 003 os模块 example

    import os for tmpdir in ('/tmp',r'C:/Users/Administrator/PycharmProjects/'): if os.path.isdir(tmpdir ...

  6. javaweb项目中获取项目名称

    request.getServletContext().getContextPath() 增加项目名称是test.那么上面的结果就是/test

  7. uboot 解压缩

    在uboot中进行解压缩是非常实用的 uboot中完毕delay 用户进行交互的段 if(BootType == '3') { char *argv[3]; printf("   \n3: ...

  8. ArraySegment

    第一个构造函数 Initializes a new instance of the ArraySegment<T> structure that delimits all the elem ...

  9. Android定时任务

    前言 我们在平常的开发中可能会遇到一些需求,比如说,每日定时提醒,定时更新数据等等,反正就是周期性任务,碰到这类需求,我们就可以叫做定时任务.以前我们可以通过使用线程Handler来实现,现在既然是在 ...

  10. mac os lscpu 【转】

    CPU Information on Linux and OS X This is small blog post detailing how to obtain information on you ...