因为对自己不太自信,所以才报了甲级- -
然后从后往前刷题,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 athttps://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 <= 1000), 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
这道题纯递归啊,判断大小堆 ==> 递归; 后续遍历 ==>递归。
//
// main.cpp
// PTA Advanced
//
// Created by air on 2018/6/13.
// Copyright © 2018年 air. All rights reserved.
#include<iostream>
using namespace std;
bool minCheck(int * a, int i, int len){
if(i > len/2 - 1) //递归出口
return true;
else{
int child = 2 * i + 1;
if(child + 1 < len && a[child] > a[child+1] )
child++;
if(a[child] < a[i])
return false;
return minCheck(a, i * 2 + 1, len) && minCheck(a, i * 2 + 2, len); //递归判断左子树和右子树
}
}
bool isMin(int * a, int len){
return minCheck(a, 0,len);
}
bool maxCheck(int * a, int i, int len){
if(i > len/2 - 1) //递归出口
return true;
else{
int child = 2 * i + 1;
if(child + 1 < len && a[child] < a[child+1] )
child++;
if(a[child] > a[i])
return false;
return maxCheck(a, i * 2 + 1, len) && maxCheck(a, i * 2 + 2, len); //递归判断左子树和右子树
}
}
bool isMax(int * a, int len){
return maxCheck(a, 0,len);
}
void postorder(int * a, int len, int begin, int * b, int & count){
if(begin > len -1) //递归的出口应该是0个元素
return;
if(begin * 2 + 1 < len)
postorder(a, len, begin * 2 + 1, b, count); //递归左子树
if(begin * 2 + 2 < len)
postorder(a, len, begin * 2 + 2, b, count); //递归右子树
b[count++] = a[begin];
}
void print(int *a, int len){
for(int i = 0; i < len - 1; i++)
cout << a[i] << " ";
cout << a[len -1] << endl;
}
int main(){
int M, N;
cin >> M >> N;
int * num = new int[N];
while(M --){
for(int i = 0; i < N; i++)
cin >> num[i];
if(isMax(num , N))
cout << "Max Heap" << endl;
else if(isMin(num ,N))
cout << "Min Heap" << endl;
else
cout << "Not Heap" << endl;
int * b = new int[N];
int count = 0;
postorder(num, N, 0, b, count);
print(b,N);
}
return 0;
}