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. LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

    题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without ...

  2. koa2使用&&中间件&&angular2的koa托管

    文章导航 1,koa2使用: 2,写中间件: 3,koa2路由配置angular2; 一.先上代码,一篇,看完koa2中大多基础方法: const Koa=require('koa'); const ...

  3. 随机森林和adaboost算法(待更新)

    Adaboost是一种迭代算法,其核心思想是针对同一个训练集训练不同的分类器(弱分类器),然后把这些弱分类器集合起来,构成一个更强的最终分类器(强分类器).

  4. socket长连接和短链接区别

    短连接 连接->传输数据->关闭连接 HTTP是无状态的,浏览器和服务器每进行一次HTTP操作,就建立一次连接,但任务结束后就中断连接.短连接是指SOCKET连接后发送后接收完数据后马上断 ...

  5. SQLSERVER store procedure 临时表

    有些时候显示重复数据时,使用: ) 但有些时候表A过大或者逻辑复杂.显示数据时,会造成性能的影响,这时你就可以使用临时表:   ) create table #temp( XXX , XXX) )in ...

  6. Qt中切换窗口功能的实现

    两条语句就能够实现了: this->newNC.setWindowFlags(Qt::WindowStaysOnTopHint); this->newNC.show(); mark一下,防 ...

  7. JavaScript能做的事 是不是jQuery都能做>?

    比如修改表单中的action 并实现跳转  问题是此时没有点击表单中的submit 提交按钮?

  8. 剑指offer--32.把数组排成最小的数

    用to_string()将整形转化为字符串,对字符串进行比较 --------------------------------------------------------------------- ...

  9. 深入Guerrilla Games解密次世代开山大作《杀戮地带暗影坠落》(The technology of Killzone Shadow Fall)

    文章摘要:这几天终于有时间,把全文翻译完了,自己感觉不是太满意,不过大家能看懂就好,就当一个学习的机会.整篇文章通过SONY第一方游戏工作室Guerrilla Games主创的语录,为我们展现了次世代 ...

  10. Developing IOS Application with Delphi Xe4 .only for play the toy?

    Recently, i am working on r&d of some keypoint of some app idea. if all thing ok, i will continu ...