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))

One thing for sure is that all the keys along any path from the root to a leaf in a max/min heap must be in non-increasing/non-decreasing order.

Your job is to check every path in a given complete binary tree, in order to tell if it is a heap or not.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (1), the number of keys in the tree. Then the next line 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, first print all the paths from the root to the leaves. Each path occupies a line, with all the numbers separated by a space, and no extra space at the beginning or the end of the line. The paths must be printed in the following order: for each node in the tree, all the paths in its right subtree must be printed before those in its left subtree.

Finally 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.

Sample Input 1:

8
98 72 86 60 65 12 23 50

Sample Output 1:

98 86 23
98 86 12
98 72 65
98 72 60 50
Max Heap

Sample Input 2:

8
8 38 25 58 52 82 70 60

Sample Output 2:

8 25 70
8 25 82
8 38 52
8 38 58 60
Min Heap

Sample Input 3:

8
10 28 15 12 34 9 8 56

Sample Output 3:

10 15 8
10 15 9
10 28 34
10 28 12 56
Not Heap
Solution:
  这道题很简单,和前面的一道题类似
  抓住两个重要条件:
    一个是大根堆,小根堆的特点
    一个是完全二叉树的性质
  然后通过层序遍历序列重构二叉树
  通过序列中第一个数与第二个数的大小比较就可以知道是大根堆还是小根堆【注意,一般不要相信题目中所谓的等于,因为PAT中的节点值就从来没有等于过】
  通过判断节点与其孩子节点的值的大小可知是否满足Heap Tree的性质
  最后使用DFS来输出路径,记得先右再左
  
 #include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
struct Node
{
int val;
Node *l, *r;
Node(int a = ) :val(a), l(nullptr), r(nullptr) {}
};
int n;
vector<int>level;
Node *creatTree(int index)//重构二叉树
{
Node *root = new Node(level[index++]);
queue<Node*>q;
q.push(root);
while (!q.empty() && index<n)
{
Node *p = q.front();
q.pop();
p->l = new Node(level[index++]);
q.push(p->l);
if (index >= n)break;
p->r = new Node(level[index++]);
q.push(p->r);
}
return root;
}
vector<int>res;
void DFS(Node *root, bool isMaxHeap,bool &isHeap)
{
if (root == nullptr)
return;
res.push_back(root->val);
if (isMaxHeap)//大根堆判断
{
if ((root->l && root->l->val > root->val) || (root->r && root->r->val > root->val))
isHeap = false;
}
else//小根堆判断
{
if ((root->l && root->l->val < root->val) || (root->r && root->r->val < root->val))
isHeap = false;
}
if (root->l == nullptr && root->r == nullptr)//输出路径
{
for (int i = ; i < res.size(); ++i)
cout << (i == ? "" : " ") << res[i];
cout << endl;
}
DFS(root->r, isMaxHeap, isHeap);//记得先右再左
DFS(root->l, isMaxHeap, isHeap);
res.pop_back();
}
int main()
{
cin >> n;
level.resize(n);
for (int i = ; i < n; ++i)
cin >> level[i];
Node* root = creatTree();
bool isHeap = true;
bool isMaxHeap = level[] >= level[] ? : ;
DFS(root, isMaxHeap, isHeap);
if (isHeap && isMaxHeap)
cout << "Max Heap" << endl;
else if (isHeap && !isMaxHeap)
cout << "Min Heap" << endl;
else
cout << "Not Heap" << endl;
return ;
}
原谅孩子不会静态重构二叉树吧 :), 静态重构【就是根据序列数位置得到整个数树的形状】是我的硬伤,相信不久的明天我就学会了 ^_^
这里借用一下别人静态重构的代码吧
 #include <iostream>
#include <vector>
using namespace std;
vector<int> v;
int a[], n, isMin = , isMax = ;
void dfs(int index) {
if (index * > n && index * + > n) {
if (index <= n) {
for (int i = ; i < v.size(); i++)
printf("%d%s", v[i], i != v.size() - ? " " : "\n");
}
}
else {
v.push_back(a[index * + ]);
dfs(index * + );
v.pop_back();
v.push_back(a[index * ]);
dfs(index * );
v.pop_back();
}
}
int main() {
cin >> n;
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
v.push_back(a[]);
dfs();
for (int i = ; i <= n; i++) {
if (a[i / ] > a[i]) isMin = ;
if (a[i / ] < a[i]) isMax = ;
}
if (isMin == )
printf("Min Heap");
else
printf("%s", isMax == ? "Max Heap" : "Not Heap");
return ;
}

  

最新文章

  1. Dijkstar算法的数学原理
  2. c#开发工具软件集合
  3. HDU 1005 Number Sequence(AC代码)
  4. 【BZOJ】【3238】【AHOI2013】diff(差异)
  5. 最短路变形 poj3615&amp;
  6. 旧版Xcode下载地址
  7. linux,安装软件报错cannot create regular file &#39;/usr/local/man/man1&#39;: No such file or directory
  8. 【拓扑排序】【HDU3231】【Box Relations】
  9. 使用函数指针和多态代替冗长的if-else或者switch-case
  10. 微信小程序的开发环境搭建(Windows版本)
  11. 八爪鱼采集器︱爬取外网数据(twitter、facebook)
  12. Day4_名称空间与作用域
  13. Spring Security OAuth2 Demo -- good
  14. 大家都知道fastclick能解决300ms延迟,现在我们来看一下,使用方法
  15. 第二篇*2、Python字符串格式化
  16. jenkins可选插件为空的解决方式
  17. URL加载jar
  18. json格式转换(json,csjon)(天气预报)
  19. python基础语法2
  20. xctf一道反序列化题

热门文章

  1. flask项目中使用富文本编辑器
  2. python学习第七天流程控制循环while和循环for区别
  3. k8s入门教程
  4. ASE Alpha Sprint - backend scrum 6
  5. JavaScript面向对象编程(2)-- 类的定义
  6. 对象名 XXX 无效。
  7. docker运行模式图
  8. Java并发(基础知识)—— Java中断机制
  9. [CentOS]安装软件:/lib/ld-linux.so.2: bad ELF interpreter 解决
  10. 大数据之hadoop框架知识