这题太太太坑爹了,不建树感觉不出来,写的很轻松,建了树就会发现题目是真的坑;
题目里明确讲是N distinct integer,但是在设计测试用例的时候,最后一个测试点在同一颗树里其实是有相同的数的;
感觉自己被出题的玩了@_@
思路:
1.思路很简单,每一树的序列,第i个和第2i+1、2i+2比较大小(如果这两个存在的话);
2.递归后序遍历二叉树;
代码:
#include<iostream>
#include<vector>
using namespace std;
int m,n;
vector<int> v;
void postOrder(int root){
if(root*2+1<n) postOrder(root*2+1);
if(root*2+2<n) postOrder(root*2+2);
root==0?printf("%d
",v[root]):printf("%d ",v[root]);
}
int main(){
scanf("%d%d",&m,&n);
v.resize(n);
for(int i=0;i<m;i++){
for(int i=0;i<n;i++) scanf("%d",&v[i]);
int sign=v[0]>v[1]?1:-1;
for(int j=0;2*j+1<n;j++)
for(int k=1;k<=2;k++)
if(2*j+k<n) sign=(v[j]-v[2*j+k])*sign<0?0:sign;
char c[][9]={"Min Heap","Not Heap","Max Heap"};
printf("%s
",c[sign+1]);
postOrder(0);
}
return 0;
}