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 ...
随机推荐
- Asp.Net Core 第04局:依赖注入
总目录 前言 本文介绍Asp.Net Core中默认的依赖注入(DI)模式. 环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 开局 第一手:依赖注入说明 1.一个 ...
- Python 多进程异常处理
前言 最近项目用到了Python作为网站的前端,使用的框架是基于线程池的Cherrypy,但是前端依然有一些比较‘重’的模块.由于python的多线程无法很好的利用多核的性质,所以觉得把这些比较‘重’ ...
- day 109结算中心.
from django.db import models from django.contrib.contenttypes.fields import GenericForeignKey,Generi ...
- Docker 清理容器 log 日志
原文 Docker 清理容器 log 日志 docker logs <容器ID> 是常用命令,来查看容器运行日志,但时间长了之后,就会发现越来越慢,log 太多了,这时就需要清理一下. 先 ...
- centons6升级gcc和glibc版本
一.先升级gcc 这里配置yum源来升级 centos6系列更换阿里yum源 1.首先备份原来的cent os官方yum源 cp /etc/yum.repos.d/CentOS-Base.repo / ...
- Java中的动态代理(jdk和cglib)
JAVA的动态代理 代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托类 ...
- MYSQL 查询脚本优化
业务需要,优化一段多表查询脚本. 总结下来,采取以下步骤. 分析语句 分析语句,了解逻辑,是否可以先优化逻辑. 查询语句的查询范围,是否是全表查询,如果是,尽量优化为按索引查询. 查看语句数量,是否有 ...
- spring整合mybatis后,mybatis一级缓存失效的原因
一般来说,可以在5个方面进行缓存的设计: 最底层可以配置的是数据库自带的query cache, mybatis的一级缓存,默认情况下都处于开启状态,只能使用自带的PerpetualCache,无法配 ...
- 第十三章 存储之volume
容器磁盘上的文件的生命周期是短暂的,这就使得在容器中运行重要应用时会出现一些问题.首先,当容器崩溃时,kubelet 会重启它,但是容器中的文件将丢失——容器以干净的状态(镜像最初的状态)重新启动.其 ...
- kvm的img文件的本机挂载
非lvm分区挂载方法: mount -o loop xxx.img /mnt/xxx 系统提示: “mount: you must specify the filesystem type” 执行:fd ...