All problems
Check Min-Heap Property
Problem
Check whether a list satisfies the min-heap property.
For every index i, both children at 2*i + 1 and 2*i + 2 must be greater than or equal to heap[i] when they exist. An empty list and a single element are valid heaps.
Return True if the min-heap property holds, otherwise False.
Hint
It is enough to compare each parent with its direct children.
Test cases
case 1
in: [[1,3,2,7,5]]
expected: true
case 2
in: [[1,2,0]]
expected: false
case 3
in: [[]]
expected: true
case 4
in: [[5]]
expected: true
case 5
in: [[2,1,3]]
expected: false
Starter codePython
def is_min_heap(heap):
# Your code here
pass5 test cases · run your code against them in the editor