1155.Heap Paths-PAT甲级真题(DFS+堆和二叉树的概念)

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<N≤1,000), 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

-----------------------------------------------------------

注意:

完全二叉树的定义:若设二叉树的深度为h,除第h层外,其他各层(1~h-1)的节点数都达到最大个数,第h层所有节点都连续集中在最左边(就是说可能会有只有左子树没有右子树的情况,按照宽度优先遍历的时候搜索到空节点时树一定已经遍历完)。完全二叉树是由满二叉树而引出来的。对于深度为k的,由n个节点的二叉树,当且仅当其每一个节点的深度都与深度为k的二叉树中编号从1到n的节点一一对应称之为完全二叉树。

题目大意:

  给出一颗完全二叉树,打印从根节点开始到所有叶节点的路径,打印的顺序先右后坐,及先序遍历的镜像。然后判断是否是堆。

分析:

1.深度打印所有的路径(从右向左,及先序的镜像),vector保存一条路径上的所有节点,通过push和pop实现回溯,维护路径,index<=n是对只有左节点没有右节点的情况的特判。

2.判断是否为堆:从第二个节点开始遍历,如果比父节点小,就不是小顶堆,如果比父节点大,就不是大顶堆。

#include<stdio.h>
#include<vector>
#pragma warning(disable:4996)
#define maxn 1004
using namespace std;
int n;
int he[maxn];
vector<int>road;
void dfs(int index)
{
    if (index * 2 > n) {
        if (index <= n) {
            for (int i = 0; i < road.size(); i++) {
                printf("%d%s", he[road[i]], i == road.size()-1 ? "\n" : " ");
            }
        }//对只有左子树没有右子树的特判

    }
    else {
        road.push_back(index*2+1);
        dfs(index * 2 + 1);
        road.pop_back();
        road.push_back(index * 2);
        dfs(index * 2);
        road.pop_back();
    }
}
int main()
{
    scanf("%d",&n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &he[i]);
    }
    road.push_back(1);
    dfs(1);
    int ismax = 1, ismin = 1;
    for (int i = 2; i <= n; i++) {
        if (he[i / 2] > he[i])ismin = 0;
        if (he[i / 2] < he[i])ismax = 0;
    }
    printf("%s",ismin ==1?"Min Heap":(ismax == 1?"Max Heap":"Not Heap"));
    getchar();
    getchar();
    return 0;
}

原文地址:https://www.cnblogs.com/zxzmnh/p/11629929.html

时间: 2024-07-30 05:24:34

1155.Heap Paths-PAT甲级真题(DFS+堆和二叉树的概念)的相关文章

1085. Perfect Sequence (25)-PAT甲级真题

1085. Perfect Sequence (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng Given a sequence of positive integers and another positive integer p. The sequence is said to be a "perfect sequence" if M <= m * p where M and m

1020. Tree Traversals (25) PAT甲级真题

之前我看了这道题,实在是看不懂网上的解题答案,他们的具体思路基本上就是通过后续遍历和中序遍历,直接推出层次遍历. 我苦思冥想了半天,是在没看懂这种思路,于是想了一个笨点的但是也比较好理解的思路,通过后续和中序,先推出整个二叉树,再考虑 对二叉树层次遍历. 本题还有一点要注意的时在输出结果的末尾,如果使用了类似 pirntf("%d ",data); 这样的格式是不对的,一定要对末尾进行判断消除最尾端的空格. 首先最核心的部分是通过两次遍历反推回二叉树:这里的思路是,后续遍历的最末尾,一

1078. Hashing (25)-PAT甲级真题

1078. Hashing (25)The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be "H(key) = key % TSize" where TSize is the

PAT甲题题解-1111. Online Map (30)-PAT甲级真题(模板题,两次Dijkstra,同时记下最短路径)

题意:给了图,以及s和t,让你求s到t花费的最短路程.最短时间,以及输出对应的路径.   对于最短路程,如果路程一样,输出时间最少的. 对于最短时间,如果时间一样,输出节点数最少的.   如果最短路程和最短时间路径一样,合并输出一次即可. 纯粹就是练习dijkstra,没什么难的. 第一次dijkstra求最短路程,记录下每个节点的路程和时间. 第二次dijkstra求最短时间,记录下每个节点的时间和经过的节点数. pre数组用来存储前驱节点,保存路径 #include <iostream>

PAT 甲级真题题解(121-155)

1121 Damn Single 模拟 1 // 1121 Damn Single 2 #include <map> 3 #include <vector> 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 using namespace std; 8 9 map<int, int> m, vis; 10 vector<int> p; 11

1132 Cut Integer PAT 甲级真题

#include<iostream> #include<sstream>#include<string>using namespace std;int n;bool judge(string s){ long long t1, t2, t3; stringstream ss; /*ss.clear(); ss << s; ss >> t1; string s1 = s.substr(0,(int) s.size() / 2); ss.clear(

1129 Recommendation System PAT 甲级真题(输出格式,set使用)

Recommendation system predicts the preference that a user would give to an item. Now you are asked to program a very simple recommendation system that rates the user's preference by the number of times that an item has been accessed by this user. Inp

1054. 求平均值 (20)-PAT乙级真题

今天刚刚到学校,2017年学习正式开始了,今天看到了浙大的<数据结构>这学期又要开课了,决定一定要跟着学习一遍:在大学生mooc网上学习:http://www.icourse163.org/course/zju-93001#/info :然后就是跟着<算法之美>也要同步看完. 然后就在PAT上随便做一道题,这是第一次通过AC,发现了两个比较好的博客主页:http://www.liuchuo.net/  和  https://www.joyhwong.com/   都总结了刷题的过程

PAT 乙级真题 1002.数字分类

PAT 乙级真题 1002.数字分类 题目描述 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...: A3 = 被5除后余2的数字的个数: A4 = 被5除后余3的数字的平均数,精确到小数点后1位: A5 = 被5除后余4的数字中最大数字. 输入格式 每个输入包含1个测试用例.每个测试用例先给出一个不超过1000的正整数N,随后给出N个不超过1000的