DFS
Tree paths and exhaustive search.
6 pages
- Pattern 09: DFS (Depth-First Search)Pattern guide
DFS is one recursive shape reused four ways. Pick your traversal order (when you touch the node) and whether you return a value up or carry state down.
- LC 104Maximum Depth of Binary Tree (LC 104)Easy
Recursive "Bubble Up" — ask children for their height, add 1. Problem walkthrough from the DFS pattern set — Java templates, complexity analysis and the…
- LC 236Lowest Common Ancestor of a Binary Tree (LC 236)Medium
Use recursion to "bubble up" found nodes; the split point is the LCA. Problem walkthrough from the DFS pattern set — Java templates, complexity analysis and…
- LC 226Invert Binary Tree (LC 226)Easy
Mirror the tree by swapping each node’s left and right children, then apply the same rule to every subtree.
- LC 100Same Tree (LC 100)Easy
Two trees are the same only if roots match and both left pairs and right pairs match recursively.
- LC 112Path Sum (LC 112)Easy
Decrement a running “remaining sum” along each root-to-leaf path; at a leaf, remaining must be exactly zero.