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 <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int n, m;
vector<int>level, post;
struct Node
{
int val;
Node *l, *r;
Node(int a = ) :val(a), l(nullptr), r(nullptr) {}
};
Node* creatTree(bool &flag, const bool isMax)
{
Node* root = new Node(level[]);
int k = ;
queue<Node*>q;
q.push(root);
while (k < m)
{
Node *p = q.front();
q.pop();
p->l = new Node(level[k++]);
if (isMax && p->val<p->l->val || !isMax && p->val>p->l->val)
flag = false;
q.push(p->l);
if (k >= m)break;
p->r = new Node(level[k++]);
if (isMax && p->val < p->r->val || !isMax && p->val > p->r->val)
flag = false;
q.push(p->r);
}
return root;
}
void postOrder(Node *root)
{
if (root == nullptr)
return;
postOrder(root->l);
postOrder(root->r);
post.push_back(root->val);
}
int main()
{
cin >> n >> m;
while (n--)
{
level.clear();
level.resize(m);
post.clear();
int minN = INT32_MAX, maxN = -;
for (int i = ; i < m; ++i)
{
cin >> level[i];
minN = minN < level[i] ? minN : level[i];
maxN = maxN > level[i] ? maxN : level[i];
}
bool flag = true, isMax = false;
Node *root = nullptr;
if (level[] == minN)//小根堆
{
isMax = false;
root = creatTree(flag, isMax);
}
else if (level[] == maxN)
{
isMax = true;
root = creatTree(flag, isMax);
}
else
{
flag = false;
root = creatTree(flag, isMax);
}
postOrder(root);
if (flag && isMax)
printf("Max Heap\n");
else if (flag && !isMax)
printf("Min Heap\n");
else
printf("Not Heap\n");
for (int i = ; i < m; ++i)
cout << (i == ? "" : " ") << post[i];
cout << endl;
}
return ;
}

第二种方法,简单点,通过完全二叉树的性质,直接判断并得出后序遍历结果

 #include <iostream>
#include <vector>
using namespace std;
int n, m;
vector<int>level, post;
void postOrder(int index)
{
if (index >= m)return;
postOrder(index * + );
postOrder(index * + );
post.push_back(level[index]);
}
int main()
{
cin >> n >> m;
while (n--)
{
level.resize(m);
for (int i = ; i < m; ++i)
cin >> level[i];
bool isMaxHeap = level[] >= level[] ? true : false;
bool flag = true;
for (int i = ; i < (m - ) / && flag; ++i)
{
int L = i * + , R = i * + ;
if (isMaxHeap && (level[i] < level[L] || R < m && level[i] < level[R]))
flag = false;
if (!isMaxHeap && (level[i] > level[L] || R<m && level[i] > level[R]))
flag = false;
}
if (flag && isMaxHeap)
printf("Max Heap\n");
else if (flag && !isMaxHeap)
printf("Min Heap\n");
else
printf("Not Heap\n");
postOrder();
for (int i = ; i < m; ++i)
cout << (i == ? "" : " ") << post[i];
cout << endl;
}
return ;
}

PAT甲级——1147 Heaps【30】的更多相关文章

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

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

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

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

  3. PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

    1057 Stack (30 分)   Stack is one of the most fundamental data structures, which is based on the prin ...

  4. pat 甲级 1057 Stack(30) (树状数组+二分)

    1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...

  5. 1147. Heaps (30)

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

  6. PAT甲级 堆 相关题_C++题解

    堆 目录 <算法笔记>重点摘要 1147 Heaps (30) 1155 Heap Paths (30) <算法笔记> 9.7 堆 重点摘要 1. 定义 堆是完全二叉树,树中每 ...

  7. [PAT] 1147 Heaps(30 分)

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

  8. PAT 1147 Heaps[难]

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

  9. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

随机推荐

  1. 1381. 删除 (Standard IO)

    题目描述: Alice上化学课时又分心了,他首先画了一个3行N列的表格,然后把数字1到N填入表格的第一行,保证每个数只出现一次,另外两行他也填入数字1到N,但不限制每个数字的出现次数.Alice现在想 ...

  2. mysql FROM_UNIXTIME 时间不准确

    mysql 使用 FROM_UNIXTIME 函数计算出来的时间少了6个小时或者8个小时 解决办法: 添加 default-time_zone = '+8:00' 这个再配置文件中 vi /etc/m ...

  3. Spark自定义维护kafka的offset到zk

    import kafka.common.TopicAndPartition import kafka.message.MessageAndMetadata import kafka.serialize ...

  4. BZOJ 2122 [分块+单调栈+二分](有详解)

    题面 传送门 给定序列d和lim.假设有一个初始价值\(x_0\),则经历第i天后价值变为\(min(x_0+d[i],lim[i])\),记\(f(i,j,x_0)\)表示以初始代价x0依次经过第i ...

  5. Flask-Scrip

    介绍及安装 Flask-Script是一个让你的命令行支持自定义命令的工具,它为Flask程序添加一个命令行解释器.可以让我们的程序从命令行直接执行相应的程序. 安装 pip install Flas ...

  6. 45-python基础-python3-字符串-常用字符串方法(三)-startswith()-endswith()

    4-字符串方法 startswith()和 endswith() startswith()和 endswith()判断字符串是否以某个字符串开始或结尾,存在返回 True,否则,方法返回 False. ...

  7. 用u盘和iso镜像文件装win8.1系统

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/xyl295528322/article/details/37910939 原料: 1.老毛桃U盘启动 ...

  8. ASP.NET Core 2.1 JWT token (一) - 简书

    原文:ASP.NET Core 2.1 JWT token (一) - 简书 JwtBearer认证是一种标准的,通用的,无状态的,与语言无关的认证方式.Bearer验证属于HTTP协议标准验证. 如 ...

  9. 第八组Postmortem事后分析

    第八组Postmortem事后分析 一.团队成员总结的改进和教训 隆晋威:Beta阶段完善架构设计,分工更加明确,文档更丰富,交流带来开销减少.Alpha技术选型不固定,分工混乱,没有方便的测试引擎, ...

  10. sqlacodegen:通过mysql语句生成sqlalchemy的model

    引用网页描述:这个工具读取现有数据库的结构并生成相应的SQLAlchemy模型代码. 使用方法详细描述在如下连接中. 先简要介绍使用方法: 安装:pip install  sqlacodegen sq ...