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 ;
}

  

PAT甲级——A1155 HeapPaths【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 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

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

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

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

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

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

  5. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  6. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  7. PAT甲级1119. Pre- and Post-order Traversals

    PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...

  8. PAT甲级1057. Stack

    PAT甲级1057. Stack 题意: 堆栈是最基础的数据结构之一,它基于"先进先出"(LIFO)的原理.基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素).现在 ...

  9. PAT甲级1026. Table Tennis

    PAT甲级1026. Table Tennis 题意: 乒乓球俱乐部有N张桌子供公众使用.表的编号从1到N.对于任何一对玩家,如果有一些表在到达时打开,它们将被分配给具有最小数字的可用表.如果所有的表 ...

随机推荐

  1. 2、单线性变量的回归(Linear Regression with One Variable)

    2.1 模型表示 我们通过一个例子来开始:这个例子是预测住房价格的,我们要使用一个数据集,数据集包含俄勒冈州波特兰市的住房价格.在这里,我要根据不同房屋尺寸所售出的价格,画出我的数据集.比方说,如果你 ...

  2. BFS+打印路径

    题目是给你起点sx,和终点gx:牛在起点可以进行下面两个操作: 步行:John花一分钟由任意点X移动到点X-1或点X+1. 瞬移:John花一分钟由任意点X移动到点2*X. 你要输出最短步数及打印路径 ...

  3. Node.js 中监听 redis key 过期事件

    It is in fact possible to listen to the “expired” type keyevent notification using a subscribed clie ...

  4. linux设置python虚拟环境的环境变量

    针对 linux系统中 python虚拟环境 设置环境变量 2种方法: 1.在建好的虚拟环境的 venv/bin/active 文件中,写入需要的环境变量,再进入虚拟环境: 如 配置文件路径 JERR ...

  5. 一、表单和ajax中的post请求&&后台获取数据方法

    一.表单和ajax中的post请求&&后台获取数据方法 最近要做后台数据接收,因为前台传来的数据太过于混乱,所以总结了一下前台数据post请求方法,顺便写了下相对应的后台接收方法. 前 ...

  6. 前后端分离下的CAS跨域流程分析

    写在最前 前后端分离其实有两类: 开发阶段使用dev-server,生产阶段是打包成静态文件整个放入后端项目中. 开发阶段使用dev-server,生产阶段是打包成静态文件放入单独的静态资源服务器中, ...

  7. Maven POM中的各种scope的行为总结

    compile:默认的scope.任何定义在compile scope下的依赖将会在所有的class paths下可用.maven工程会将其打包到最终的artifact中.如果你构建一个WAR类型的a ...

  8. Java Web学习总结(3)Servlet(二)

    一,Servlet访问URL映射配置 由于客户端是通过URL地址访问web服务器中的资源,所以Servlet程序若想被外界访问,必须把servlet程序映射到一个URL地址上,这个工作在web.xml ...

  9. SQL Server 2005 的动态管理视图DMV和函数DMF

    优化 的动态管理视图DMV和函数DMF SQL Server 05提供了动态管理视图Dynamic Management Views和函数 Functions,方便了我们对系统运行情况的监控,故障诊断 ...

  10. 从React Native到微服务,落地一个全栈解决方案

    Poplar是一个社交主题的内容社区,但自身并不做社区,旨在提供可快速二次开发的开源基础套件.前端基于React Native与Redux构建,后端由Spring Boot.Dubbo.Zookeep ...