Trees & Recursion
Tree DFS · Postorder
When you see it
Signal words
If the prompt uses any of these, this pattern should come to mind first.
postorderheightdepthdiametersubtree sumdelete treebottom-upcombine children
Practice
Drill this pattern with an AI trainer — reply only with Hint, Next, Lost, or paste your code.
Ask yourself
- Do values bubble up from children — height, sums, diameter?
- Tearing a tree down (delete/free) so children must go first?
- Recurse in order: left, then right, then visit — combine child results.
Classic problem
Binary Tree Postorder Traversal — Visit both subtrees before root: left → right → root. Used when processing depends on children (e.g., tree deletion, diameter computation).
Input: Binary tree with nodes A(root), B(left), C(right), D(B-left), E(B-right), F(C-left), G(C-right)
Output: [D, E, B, F, G, C, A] — children always processed before parent.
Common pitfalls
Forgetting the null base case.
Combining child results in the wrong order or way.