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 ...
随机推荐
- Nginx允许跨域访问的配置问题
网站项目动静分离,静态资源服务器A 业务服务器B B中静态资源由A加载 出现如下问题: @font-face { font-family: 'iconfont'; src: url('../font ...
- js 使用技巧
一,获取客户端状态 1.获取cookie function cookieInfo() { setcookie('cookie_test','1'); var cookie_test = getcook ...
- C# 获取微信小程序access_token
/// <summary> /// 获取access_token /// </summary> /// <returns></returns> publ ...
- 配置访问公网主机上的jupyter notebook
文章结构: 一.安装python 二.安装并配置jupyter并配置jupyter 三.第一个python程序 一.安装python 1.1下载python安装包 # wget https://www ...
- [fw]谈EXPORT_SYMBOL使用
EXPORT_SYMBOL只出现在2.6内核中,在2.4内核默认的非static 函数和变量都会自动导入到kernel 空间的, 都不用EXPORT_SYMBOL() 做标记的.2.6就必须用EXPO ...
- Neo4j百万级数据导入只需30s
先上图:425万nodes.180万relationships只用了30s 243ms 项目需要生成关系图,开始考虑的是用Neo4j官网提供的REST API,从solr中查出2组数据先创建节点再创建 ...
- 【笔记目录1】【jessetalk 】ASP.NET Core快速入门_学习笔记汇总
当前标签: ASP.NET Core快速入门 共2页: 1 2 下一页 任务50:Identity MVC:DbContextSeed初始化 GASA 2019-03-02 14:09 阅读:16 ...
- 【记录】MongoDB
什么情况建议使用MongoDB? 1:满足对数据库的高并发读写 2:对海量数据的高效存储和访问 3:对数据库高扩展性和高可用性 4:灵活的数据结构,满足数据结构不固定的场景 5:应用需要2000-30 ...
- 七、Null、空以及0的区别
一.Null的区别 create database scort use scort create table emp ( empno int primary key, ename ), sal int ...
- Hibernate中Session的save()、update()、merge()、lock()、saveOrUpdate()和persist()方法有什么区别?
Hibernate的对象有三种状态:瞬态.持久态和游离态.游离状态的实例可以通过调用save().persist()或者saveOrUpdate()方法进行持久化:脱管状态的实例可以通过调用 upda ...