Adding a detailed solution with support for java generics is relatively easy to follow.
public static <T extends Comparable<T>> boolean isMinHeap(T arr[], int rootIndex) { boolean isMaxH = true; int lChild = 2 * rootIndex + 1; int rChild = 2 * rootIndex + 2; // Nothing to compare here, as lChild itself is larger then arr length. if (lChild >= arr.length) { return true; } if (arr[rootIndex].compareTo(arr[lChild]) > 0) { return false; } else { isMaxH = isMaxH && isMinHeap(arr, lChild); } // rChild comparison not needed, return the current state of this root. if (rChild >= arr.length) { return isMaxH; } if (arr[rootIndex].compareTo(arr[rChild]) > 0) { return false; } else { isMaxH = isMaxH && isMinHeap(arr, rChild); } return isMaxH; }
source share