PAT甲级——A1155 HeapPaths【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))

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来输出路径,记得先右再左  
 1 #include <iostream>
 2 #include <vector>
 3 #include <queue>
 4 #include <algorithm>
 5 using namespace std;
 6 struct Node
 7 {
 8     int val;
 9     Node *l, *r;
10     Node(int a = 0) :val(a), l(nullptr), r(nullptr) {}
11 };
12 int n;
13 vector<int>level;
14 Node *creatTree(int index)//重构二叉树
15 {
16     Node *root = new Node(level[index++]);
17     queue<Node*>q;
18     q.push(root);
19     while (!q.empty() && index<n)
20     {
21         Node *p = q.front();
22         q.pop();
23         p->l = new Node(level[index++]);
24         q.push(p->l);
25         if (index >= n)break;
26         p->r = new Node(level[index++]);
27         q.push(p->r);
28     }
29     return root;
30 }
31 vector<int>res;
32 void DFS(Node *root, bool isMaxHeap,bool &isHeap)
33 {
34     if (root == nullptr)
35         return;
36     res.push_back(root->val);
37     if (isMaxHeap)//大根堆判断
38     {
39         if ((root->l && root->l->val > root->val) || (root->r && root->r->val > root->val))
40             isHeap = false;
41     }
42     else//小根堆判断
43     {
44         if ((root->l && root->l->val < root->val) || (root->r && root->r->val < root->val))
45             isHeap = false;
46     }
47     if (root->l == nullptr && root->r == nullptr)//输出路径
48     {
49         for (int i = 0; i < res.size(); ++i)
50             cout << (i == 0 ? "" : " ") << res[i];
51         cout << endl;
52     }
53     DFS(root->r, isMaxHeap, isHeap);//记得先右再左
54     DFS(root->l, isMaxHeap, isHeap);
55     res.pop_back();
56 }
57 int main()
58 {
59     cin >> n;
60     level.resize(n);
61     for (int i = 0; i < n; ++i)
62         cin >> level[i];
63     Node* root = creatTree(0);
64     bool isHeap = true;
65     bool isMaxHeap = level[0] >= level[1] ? 1 : 0;
66     DFS(root, isMaxHeap, isHeap);
67     if (isHeap && isMaxHeap)
68         cout << "Max Heap" << endl;
69     else if (isHeap && !isMaxHeap)
70         cout << "Min Heap" << endl;
71     else
72         cout << "Not Heap" << endl;
73     return 0;
74 }
原谅孩子不会静态重构二叉树吧 :), 静态重构【就是根据序列数位置得到整个数树的形状】是我的硬伤,相信不久的明天我就学会了 ^_^这里借用一下别人静态重构的代码吧
 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 vector<int> v;
 5 int a[1009], n, isMin = 1, isMax = 1;
 6 void dfs(int index) {
 7     if (index * 2 > n && index * 2 + 1 > n) {
 8         if (index <= n) {
 9             for (int i = 0; i < v.size(); i++)
10                 printf("%d%s", v[i], i != v.size() - 1 ? " " : "\n");
11         }
12     }
13     else {
14         v.push_back(a[index * 2 + 1]);
15         dfs(index * 2 + 1);
16         v.pop_back();
17         v.push_back(a[index * 2]);
18         dfs(index * 2);
19         v.pop_back();
20     }
21 }
22 int main() {
23     cin >> n;
24     for (int i = 1; i <= n; i++)
25         scanf("%d", &a[i]);
26     v.push_back(a[1]);
27     dfs(1);
28     for (int i = 2; i <= n; i++) {
29         if (a[i / 2] > a[i]) isMin = 0;
30         if (a[i / 2] < a[i]) isMax = 0;
31     }
32     if (isMin == 1)
33         printf("Min Heap");
34     else
35         printf("%s", isMax == 1 ? "Max Heap" : "Not Heap");
36     return 0;
37 }

  

原文地址:https://www.cnblogs.com/zzw1024/p/11939055.html

时间: 2024-10-08 23:04:28

PAT甲级——A1155 HeapPaths【30】的相关文章

PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). No

PAT甲级1005 Spell It Right

题目:PAT甲级 1005 题解:水题.看到题目的第一时间就在想一位一位的mod,最后一加一转换就完事了.结果看到了N最大为10的100的次方,吓得我赶紧放弃这个想法... 发现碰到这种情况用字符串十分好用,这道题应该考察的就是这一点.大致思路就是把数字的每一位放到字符串中,然后通过ASCII码得到每一位的相加结果num,然后把num一位一位的放到stack中,使用stack是因为它先进先出的特性,最后输出就行了. 代码: 1 #include<cstdio> 2 #include<qu

PAT 1004. Counting Leaves (30)

A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tre

PAT 1072. Gas Station (30)

A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible.  However it must guarantee that all the houses are in its service range. Now given the map o

PAT甲级考前整理

终于在考前,刷完PAT甲级130道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种境界.PAT甲级题目总的说卡题目的比较多,卡测试点的比较少,有些题目还会有题意混淆,这点就不吐槽了吧.静下心来耍这130道题,其实磨练的是一种态度与手感,养成的是一种习惯.热爱AC没有错!! 130道题目主要的考点: 1.排序:快速排序,直接插入排序,希尔排序,分治排序,堆排序. 2.图论:拓扑排序.最短路径.深度搜索.广度搜索. 3.树:树的遍历.完全二叉树.AVL. 4.其他:并查集,模拟,哈希.背包.

PAT甲级考试题库1001 A+B Format 代码实现及相关知识学习

准备参加九年九月份的PAT甲级证书考试,对网站上的题目进行总结分析: 1001题 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). 计算a+b的值并以一定格式输出其和sum(数字需要

PAT甲级专题|最短路

PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做,只能得20分 #include<bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 510; int cmax,n,ter,m; int caps[maxn]; int g[maxn][m

【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全部出手的利润.输出最大利润. trick: 测试点2可能包含M不为整数的数据.(尽管题面说明M是正整数,可是根据从前PAT甲级题目的经验,有可能不是整数.....) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using names

【PAT 甲级】1151 LCA in a Binary Tree (30 分)

题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.Given any two nodes in a binary tree, you are supposed to find their LCA. 最小共同祖先(LCA)是一棵树中两个节点U和V最深的那个公共父节点.要求给一棵树,以及两个节点,请你