0

I have been stuck on solving this task for quite a while. I need to write a recursive function to check whether that every node is smaller than any of its children. Returns true if the binary tree is a min-heap and false otherwise.

What i have so far:

def min_heap(t):
    if t == None:
        return True
    else:
        return t.left.value > t.value and t.right.value > t.value
rafaelc
  • 57,686
  • 15
  • 58
  • 82
dustinyourface
  • 313
  • 1
  • 3
  • 9

1 Answers1

1

If it's recursive, that means it should be calling itself. Assuming your min-heap by definition is

every node is smaller than any of its children

def min_heap(t):
    if t == None:
        return True
    if t.left and t.value > t.left.value:
        return False
    if t.right and t.value > t.right.value:
        return False
    return min_heap(t.left) and min_heap(t.right)
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159