PAT甲级——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 <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】的更多相关文章
- PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)
1147 Heaps (30 分) In computer science, a heap is a specialized tree-based data structure that sati ...
- PAT Advanced 1147 Heaps (30) [堆,树的遍历]
题目 In computer science, a heap is a specialized tree-based data structure that satisfies the heap pr ...
- PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the prin ...
- pat 甲级 1057 Stack(30) (树状数组+二分)
1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the princi ...
- 1147. Heaps (30)
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- PAT甲级 堆 相关题_C++题解
堆 目录 <算法笔记>重点摘要 1147 Heaps (30) 1155 Heap Paths (30) <算法笔记> 9.7 堆 重点摘要 1. 定义 堆是完全二叉树,树中每 ...
- [PAT] 1147 Heaps(30 分)
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- PAT 1147 Heaps[难]
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
随机推荐
- CentOS安装系统时硬盘分区建议
一.常见挂载点的情况说明一般来说,在linux系统中都有最少两个挂载点,分别是/ (根目录)及 swap(交换分区),其中,/ 是必须的: 详细内容见下文: 安装系统时选择creat custom ...
- Visual Studio Code 键盘参考表
2019年4月6日,对照中英翻译. 一般 Ctrl+Shift+P, F1 显示命令调色板 Ctrl+P 快速打开,转到文件… Ctrl+Shift+N 新建窗口/实例 Ctrl+Shift+W ...
- [CF895E] Eyes Closed(线段树,期望)
Desctiption 传送门:Portal 大致题意: 给你一个序列, 支持两种操作: 1 l1 r1 l2 y2 在\([l1, r1]\)随机选择一个数a, \([l2, r2]\) 内随机选择 ...
- mac 支持rz sz
安装 lrzsz brew install lrzsz 配置 iTerm2 安装完成后我们需要在 iTerm2 中使用的话,还需要一些配置 进入到 /usr/local/bin 目录下,下载两个脚本文 ...
- 在Python中处理大型文件的最快方法
我们需要处理的各种目录中有大约500GB的图像.每个图像的大小约为4MB,我们有一个python脚本,一次处理一个图像(它读取元数据并将其存储在数据库中).每个目录可能需要1-4小时才能处理,具体取决 ...
- jackson 问题定位
问题背景: 云计算Pass平台版本升级,导致引用的jackson的包直接由1.*升级为2.* .在版本1.*中对于字段名与实际json不符的直接忽略了,而在2.*中则会报错.诸如此类,有较大差异,需要 ...
- Linux系统测试端口连通性的方法
Linux系统测试端口连通性的方法 有四种常用方法:1. telnet 方法2. wget 方法3. ssh 方法4. curl 方法 下面一一介绍. 1. telnet用法: telnet ip p ...
- Android面向切面编程(AOP)(转)
转自:https://www.jianshu.com/p/aa1112dbebc7 一.简述 1.AOP的概念 如果你用java做过后台开发,那么你一定知道AOP这个概念.如果不知道也无妨,套用百度百 ...
- java命令-jstat/ javap
jstat命令对应用程序资源和性能进行实时监控 常用参数列举如下: 1. jstat -class pid 显示加载class的数量.所占空间.所耗时间等信息 2.jstat -compiler pi ...
- python3.x 扯扯【切片】这玩意儿
在此之前先了解一下list这个玩意儿: list对应cpp这的数组,一维数组,二维数组,或者是嵌套都行: L=[] #空列表 L=[1,2,3,4,5,6] #六项 L=['a',['b','c']] ...