Please tell me how this recursion code works at ```is_balanced(node.left)```

def is_balanced(node):
    if node is None:
        return True, 0
    balanced_l, height_l = is_balanced(node.left)
    balanced_r, height_r = is_balanced(node.right)
    balanced = balanced_l and balanced_r and abs(height_l - height_r) <=1
    height = 1 + max(height_l, height_r)
    return balanced, height


The following tree is balanced:

1 Like

Just Imagine this tree…

       / \
      2   3

And try to go through the code line by line…depth by depth and using a pencil and paper and making the recursion Stack and try to uncover the explanation by yourself…that way you’ll understand the concept more clearly

Also I have already explained the checkbst(node) function to you so this function would work as an exercise for you…

Also when you are done with the above tree…go for the tree you have posted and try to understand how everything works…

Hope you will get it…Good Luck…

The best way for you to see would be to use a debugger. I recommend using Thonny IDE and learn how to use it’s debugger.