题目
思路
水题
代码
#include <iostream>
#include <vector>
using namespace std;
vector<int> vec;
vector<int> tmp;
bool isMax = false, isMin = false;
int n;
void DFS(int cur) {
if (cur != 1) {
if (vec[cur] > vec[cur / 2])
isMin = true;
else
isMax = true;
}
if (2 * cur > n && 2 * cur + 1 > n) {
for (int i = 0; i < tmp.size(); ++i)
i == 0 ? cout << vec[tmp[i]] : cout << " " << vec[tmp[i]];
cout << endl;
return;
}
if (2 * cur + 1 <= n) {
tmp.emplace_back(2 * cur + 1);
DFS(2 * cur+1);
tmp.pop_back();
}
if (2 * cur <= n) {
tmp.emplace_back(2 * cur);
DFS(2 * cur);
tmp.pop_back();
}
}
int main()
{
cin >> n;
vec.resize(n + 1);
for (int i = 1; i <= n; ++i) cin >> vec[i];
tmp.emplace_back(1);
DFS(1);
tmp.pop_back();
if (isMax && isMin)
cout << "Not Heap";
else if (isMax && !isMin)
cout << "Max Heap";
else
cout << "Min Heap";
return 0;
}