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. Android源码下载和编译过程

    这是我在编译android源码时整理记录的编译步骤和错误解决方法,期间参考了一些网上的博客和教程. 第一步: 安装ubuntu12.04,分配一盘空间50G,2G内存.如果分配1G内存编译时将报错.( ...

  2. mysql中删除完全重复数据的准确SQL语句

    删除数据库中重复的记录,只保留一条 DELETE FROM tb_gps_records WHERE id NOT IN (SELECT bid FROM (SELECT min(id) as bid ...

  3. java处理HTTP请求

    import com.diyfintech.wx.service.HttpService; import org.springframework.stereotype.Service; import ...

  4. L127

    Biggest Studies on Aspirin Show Risks Outweigh Benefits for Many People Doctors have long recommende ...

  5. 【dlbook】数学基础

    [代数] Moore-Penrose 伪逆 [概率信息论] 自信息,香农熵,衡量两个分布的差异:kl散度 \ 交叉熵 [数值] 溢出: softmax计算的时候要关注上溢和下溢,如果所有X都相等且为很 ...

  6. Django和pymysql搭建学员管理系统

    学员管理系统 项目规划阶段 项目背景 近年来老男孩教育的入学学员数量稳步快速增长,传统的excel统计管理学员信息的方式已经无法满足日渐增长的业务需求.因此公司急需一套方便易用的“学员管理系统”,来提 ...

  7. Leetcode 938. Range Sum of BST

    import functools # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, ...

  8. poj2378(dfs,树形dp)

    和poj3107,poj1655一样的方法 #include<iostream> #include<cstdio> #include<cstdlib> #inclu ...

  9. 【整理】石子合并问题(四边形不等式DP优化)

    有很多种算法: 1,任意两堆可以合并:贪心+单调队列. 2,相邻两堆可合并:区间DP    (O(n^3)) ). 3,相邻,四边形不等式优化DP (O(n^2) ). 4,相邻,GarsiaWach ...

  10. 深入了解ZooKeeper(二)

    在上篇博客<<深入了解ZooKeeper(一)>>中我们知道了分布式协调技术.分布式锁的实现和zookeeper服务机制,接下来将进一步了解zookeeper究竟能为我们做了什 ...