When to Use

Pattern triggers

Look for these signals in the problem:

  • Hierarchical data structure given: When the problem provides a tree, traversal patterns (preorder, inorder, postorder, level-order) apply directly.
  • "Subtree" in problem statement: Problems involving subtrees naturally decompose into recursive subproblems.
  • BST property mentioned: Binary Search Trees enable O(logn)O(\log n) search by eliminating half the tree each step.
  • "Path" from root or between nodes: Tree paths are found by combining paths through ancestors and descendants.
  • "Depth" or "height" calculations: Recursively compute depth as 11 plus max child depth.