"Here's the whole track in one sentence: because a tree is made of smaller trees, almost every tree problem is solved by 'handle this node, and trust recursion to handle the children.' Learn that template and you've learned tree algorithms."
The Template Behind Almost Everything
You saw it in counting, height, and traversal: tree algorithms share one shape. It's two parts:
- Base case — what to return for an empty subtree (usually 0, None, or True). This stops the recursion.
- Recursive case — recurse on the left subtree, recurse on the right subtree, then combine their answers with this node's own value.
That's it. Height combines with 1 + max(left, right). Sum combines with node.val + left + right. Count combines with 1 + left + right. "Is balanced" combines by checking the two subtree heights differ by ≤1. The structure is self-similar, so the solution is: solve the node, recurse on the subtrees, combine. Different problems just change the combine step.
The Leap of Faith
The mental trick that makes this easy is what the next track calls the recursive leap of faith: when writing the function, assume the recursive calls already work correctly on the smaller subtrees, and only worry about combining their results for the current node. Don't try to trace the whole recursion in your head — that way madness lies. Trust that height(node.left) returns the correct left height, and just write the one combine step. The base case guarantees the recursion bottoms out, and induction does the rest. This single mindset shift turns tree problems from intimidating to almost mechanical.
When Recursion Bites Back
One caution carried from the Complexity track: recursion uses the call stack, O(height) deep. On a balanced tree that's O(log n) — totally fine. But on a degenerate, unbalanced tree (height ≈ n), deep recursion can hit Python's recursion limit and crash with RecursionError. For trees that might be pathologically deep, you convert the recursion to iteration with an explicit stack (the stack/recursion equivalence from the Stacks track). For balanced trees — the normal case — recursion is clean and safe. Know the failure mode so it never surprises you in production.