1147 Heaps(30 分)
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 <cstdio>
int queryCnt,n;
];

void postOrderTraversal(int root)
{
    if(root>n) return;
    postOrderTraversal(root*);
    postOrderTraversal(root*+);
    printf("%d",CBT[root]);
    ) printf("\n");//因为是后序遍历,最后访问根节点,而根节点的下标固定为1
    else printf(" ");
}

int main()
{
    scanf("%d%d",&queryCnt,&n);
    ;i<queryCnt;i++){
        ;j<=n;j++)
            scanf("%d",&CBT[j]);
        //判断,层序遍历完全二叉树的非叶结点
        ]>CBT[]?:;//大顶堆标记为1,小顶堆标记为0。题目保证结点个数至少有两个。
        ;j<=n/;j++){
            ){
                ] || (j*+<=n && CBT[j]<CBT[j*+])){
                    flag=-;
                    break;
                }
            }
            ){
                ] || (j*+<=n && CBT[j]>CBT[j*+])){
                    flag=-;
                    break;
                }
            }
        }
        ) printf("Max Heap\n");
        ) printf("Min Heap\n");
        else printf("Not Heap\n");
        //输出后续序列
        postOrderTraversal();
    }
    ;
}

1147 Heaps的更多相关文章

  1. PAT 1147 Heaps[难]

    1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...

  2. [PAT] 1147 Heaps(30 分)

    1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...

  3. PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)

    1147 Heaps (30 分)   In computer science, a heap is a specialized tree-based data structure that sati ...

  4. PAT 1147 Heaps

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

  5. 1147. Heaps (30)

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

  6. PAT甲级——1147 Heaps【30】

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

  7. PAT Advanced 1147 Heaps (30) [堆,树的遍历]

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

  8. PAT (Advanced Level) 1144~1147:1145Hash二次探查 1146拓扑排序 1147堆

    1144 The Missing Number(20 分) 题意:给定N个数的序列,输出不在序列中的最小的正整数. 分析: 1.给定的N个数可能为正,可能为负,可能重复. 2.由于N≤10​5​​,所 ...

  9. PAT甲级目录

    树(23) 备注 1004 Counting Leaves   1020 Tree Traversals   1043 Is It a Binary Search Tree 判断BST,BST的性质 ...

随机推荐

  1. Spark- 使用hiveContext时提交作业报错

    在spark上操作hive时不需要搭建hive环境,只需要从现有的hive集群中hive的conf目录下拷贝 hive-site.xml 到spark的conf目录下即可提交程序运行 出现报错 Cau ...

  2. 报错 IllegalArgumentException occurred calling getter of cn.itcast.entity.Customer.cid

    我碰到这个问题的时候,没有数据类型不匹配的情况,也没有表达无法向action传递数据的情况,一直报这样的错误,意思就是无法使用Customer中的get方法来赋值.完整报错如下所示: HTTP Sta ...

  3. HYSBZ - 2301 莫比乌斯反演

    链接 题解:直接用公式算,用容斥来减掉重复计算的部分 但是我犯了一个非常sb的错误,直接把abcd除k了,这样算a-1的时候就错了,然后举的例子刚好还没问题= = ,结果wa了好几发 //#pragm ...

  4. Updated: Database Partitioning with EBS Whitepaper

    Partitioning allows a single database table and its associated indexes to be broken into smaller com ...

  5. 实验二. 使用LoadRunner进行压力测试

    实验二. 使用LoadRunner进行压力测试 一. LoadRunner 概要介绍   1.1简介   LoadRunner 是一种预测系统行为和性能的工业标准级负载测试工具.通过以模拟上千万用户实 ...

  6. USB转串口WIN8驱动安装

    http://jingyan.baidu.com/article/11c17a2c0bb606f446e39da0.html  //查看百度经验 http://jingyan.baidu.com/ar ...

  7. linux下vim对于意外退出的文档的再次开启

    转载的: 1.对于同一个文件如果上次已经打开,而未关闭的情况下,又打开该文件进行编辑时,会出现如下提醒: 这是由于已经打开但未闭关的文件,会在其目录下出现一个.swp的文件,由于是属于隐藏文件,可以用 ...

  8. LeetCode OJ:Reverse Linked List II(反转链表II)

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  9. Android 中的BroadCastReceiver

    BroadCastReceiver 简介 (末尾有源码) BroadCastReceiver 源码位于: framework/base/core/java/android.content.Broadc ...

  10. Android开发技巧——写一个StepView

    在我们的应用开发中,有些业务流程会涉及到多个步骤,或者是多个状态的转化,因此,会需要有相关的设计来展示该业务流程.比如<停车王>应用里的添加车牌的步骤. 通常,我们会把这类控件称为&quo ...