DSA · interactive
Trees: let the children answer first
Most tree problems have the same shape: ask the left child, ask the right child, then combine their answers. Watch it find the depth of a tree, one call at a time.
Step 1 of 15
running nowwaiting on the stackanswer returned
Call stack
- maxDepth(1)
Height now 1. Tallest it gets: 3, the depth of the tree.
maxDepth(1) is called. Before it can answer, it has to ask its children.
Tree
1 maxDepth(node) is a question: how many levels are there from this node down? The call goes on the stack and waits.
maxDepth(1) is waiting
2 3 4
Now solve it
In production
Tools and technologies
Trees are everywhere software keeps a hierarchy, and most code that walks them is this same recursion.
Not tied to one language — use these names when you search docs or design a system.