Patterns

TD · Pattern 08

Tree DFS

Explore tree paths using recursion (or explicit stack). Three traversal orders: pre-, in-, post-order. Great for path sums and BST validation.

Tree DFS
Time
O(n)
Space
O(h) where h = height

Spot it when

  • Root-to-leaf path sums
  • Max/min depth
  • Validate BST properties
  • All paths from root to leaf