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

Your job is to tell if a given complete binary tree is a heap.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers: M (≤ 100), the number of trees to be tested; and N (1 < N ≤ 1,000), the number of keys in each tree, respectively. Then M lines follow, each 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, 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. Then in the next line print the tree's postorder traversal sequence. All the numbers are separated by a space, and there must no extra space at the beginning or the end of the line.

Sample Input:

3 8
98 72 86 60 65 12 23 50
8 38 25 58 52 82 70 60
10 28 15 12 34 9 8 56

Sample Output:

Max Heap
50 60 65 72 12 23 86 98
Min Heap
60 58 52 38 82 70 25 8
Not Heap
56 12 34 28 9 8 15 10

#include <stdio.h>
#include <algorithm>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <queue>
using namespace std;
const int maxn=;
int a[maxn] ;
int n,m;
vector<int> v;
void dfs(int index){
v.push_back(a[index]);
if(index*>n){
if(index<=n){
for(int i=;i<v.size();i++){
printf("%d%s",v[i],i!=v.size()-?" ":"\n");
}
}
}
else{
//v.push_back(a[index*2+1]);
dfs(index*+);
//v.pop_back();
//v.push_back(a[index*2]);
dfs(index*);
//v.pop_back();
}
v.pop_back();
}
void postorder(int root){
if(root<=n){
postorder(*root);
postorder(*root+);
printf("%d%s",a[root],root==?"\n":" ");
}
}
int main(){
scanf("%d %d",&m,&n);
for(int j=;j<m;j++){
int ismax=,ismin=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=;i<=n;i++){
if(a[i/]>a[i])ismin=;
if(a[i/]<a[i])ismax=;
}
if(ismin==)printf("Min Heap\n");
else{
printf("%s\n",ismax==?"Max Heap":"Not Heap");
}
postorder();
} }

注意点:这题和1155 Heap Path 差不多,就是输出变成了后序遍历。

PAT A1147 Heaps (30 分)——完全二叉树,层序遍历,后序遍历的更多相关文章

  1. 二叉树 Java 实现 前序遍历 中序遍历 后序遍历 层级遍历 获取叶节点 宽度 ,高度,队列实现二叉树遍历 求二叉树的最大距离

    数据结构中一直对二叉树不是很了解,今天趁着这个时间整理一下 许多实际问题抽象出来的数据结构往往是二叉树的形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显 ...

  2. 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历

    [二叉树遍历模版]前序遍历     1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...

  3. [leetcode]_根据二叉树的先序遍历(后序遍历) + 中序遍历 重建二叉树

    题目1:Construct Binary Tree from Preorder and Inorder Traversal 给定一棵二叉树的先序遍历和中序遍历,求重建二叉树. 思路: 1.先序遍历的第 ...

  4. UVa 548 Tree(中序遍历+后序遍历)

    给一棵点带权(权值各不相同,都是小于10000的正整数)的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小.如果有多解,该叶子本身的权应尽量小.输入中每两行表示一棵树,其中第一行为中序遍 ...

  5. 前序遍历+中序遍历 --> 后序遍历 (二叉树)

  6. 基于visual Studio2013解决面试题之0208二叉搜索树后序遍历序列

     题目

  7. 数据结构-二叉树(1)以及前序、中序、后序遍历(python实现)

    上篇文章我们介绍了树的概念,今天我们来介绍一种特殊的树--二叉树,二叉树的应用很广,有很多特性.今天我们一一来为大家介绍. 二叉树 顾名思义,二叉树就是只有两个节点的树,两个节点分别为左节点和右节点, ...

  8. codevs2010 求后序遍历

    难度等级:白银 2010 求后序遍历 题目描述 Description 输入一棵二叉树的先序和中序遍历序列,输出其后序遍历序列. 输入描述 Input Description 共两行,第一行一个字符串 ...

  9. 利用树的先序和后序遍历打印 os 中的目录树

    [0]README 0.1)本代码均为原创,旨在将树的遍历应用一下下以加深印象而已:(回答了学习树的遍历到底有什么用的问题?)你对比下linux 中的文件树 和我的打印结果就明理了: 0.2)我们采用 ...

  10. 二叉树中序遍历,先序遍历,后序遍历(递归栈,非递归栈,Morris Traversal)

    例题 中序遍历94. Binary Tree Inorder Traversal 先序遍历144. Binary Tree Preorder Traversal 后序遍历145. Binary Tre ...

随机推荐

  1. 总结:JDK1.5-JDK1.8各个新特性

    JDK1.5-JDK1.8各个新特性 JDK各个版本的新特性 要了解一门语言,最好的方式就是要能从基础的版本进行了解,升级的过程,以及升级的新特性,这样才能循序渐进的学好一门语言.以下介绍一下JDK1 ...

  2. 在WindowsPhone开发中使用MVVM设计模式

    相信.NET程序员多多少少都听说过MVVM的设计模式,对于一个大一点的项目来说,使用这种设计模式无疑是一种不错的选择, 它提高了程序的可维护性,降低了耦合度,可以实现代码的重用,方便独立开发和进行测试 ...

  3. cache 访问频率的思考

    互联网的项目用户基数很大,有时候瞬间并发量非常大,这个时候对于数据访问来说是个灾难.为了应对这种场景,一般都会大量采用web服务器集群,缓存集群.采用集群后基本上就能解决大量并发的数据访问.当然这个时 ...

  4. C# 6 元组应用 Part 2:C# 也玩模式匹配

    C# 7给我们带来了一个半吊子的 switch 语句模式匹配,只能简单的匹配类型而已,完全没有什么用处.这里我提供个更好的简单实现,用 C# 6 的 ValueTuple 模拟函数式语言的模式匹配功能 ...

  5. 关于Bootstrap fileinput 上传新文件,移除时触发服务器同步删除的配置

    在Bootstrap fileinput中移除预览文件时可以通过配置initialPreviewConfig: [ { url:'deletefile',key:fileid } ] 来同步删除服务器 ...

  6. Oracle win32_11gR2_database在Win7下的安装与卸载

    Oracle win32_11gR2_database在Win7下的安装与卸载 by:授客 QQ:1033553122 Oracle的硬件要求 在安装oracle之前,请检查一下自己的电脑硬件是否复合 ...

  7. JavaScript大杂烩13 - 总结ECMAScript 5新功能

    虽说这个标准已经出来很久了,所有的主流浏览器的最新版本也都支持了这些特性,但是很多的教程中并没有包含这个部分,这一节我们专门来总结一下这个标准中的新功能. Object的新方法 在最新的JavaScr ...

  8. multipart/form-data文件上传

    form表单的enctype属性:规定了form表单数据在发送到服务器时候的编码方式 application/x-www-form-urlencoded:默认编码方式 multipart/form-d ...

  9. windows端ndk 编译.c/cpp文件生成so库示例

  10. [20180606]如何dump数据库里面的汉字.txt

    [20180606]如何dump数据库里面的汉字.txt --//链接http://www.itpub.net/thread-2102613-1-2.html的问题. 比如:col 41: [42]e ...