Easy Recursive Max Trees
In plain English: Find how many levels deep a binary tree goes from root to its deepest leaf.
Height is defined recursively: a node's height is 1 + the taller subtree — base case None returns -1 so leaves get height 0.
Prompt
Find the height of a binary tree.
Try to write it from scratch before scrolling down.
Solution
def height(node):
if node is None: # None has height -1, leaf has height 0
return -1
return 1 + max(height(node.left), height(node.right))
# Test: single node => 0, node with one child => 1