All Patterns

Trees

Traverse and manipulate hierarchical structures using DFS (pre/in/post-order) and BFS (level-order) traversals.

When to use

  • Hierarchical data
  • BST search/insert
  • Path problems
  • Level-order processing

When NOT to use

  • Flat data with no hierarchy
  • Need to visit nodes in arbitrary order

Common traps

  • Forgetting null checks
  • Confusing pre/in/post-order
  • Not returning values correctly in recursion

Key Invariant

Most tree problems are solved by choosing the right traversal order and passing state through recursion

Problems (22)

#20Invert Binary Tree
easyFREE
#46Binary Tree Inorder Traversal
easyFREE
#47Maximum Depth of Binary Tree
easyFREE
#48Symmetric Tree
easyFREE
#49Path Sum
easyFREE
#38Validate Binary Search Tree
mediumFREE
#77Binary Tree Level Order Traversal
medium
#78Binary Tree Zigzag Level Order Traversal
medium
#79Lowest Common Ancestor of a Binary Search Tree
medium
#80Binary Tree Right Side View
medium
#81Count Good Nodes in Binary Tree
medium
#82Diameter of Binary Tree
medium
#83Kth Smallest Element in a BST
medium
#206Flatten Binary Tree to Linked List
medium
#207Populating Next Right Pointers
medium
#209House Robber III
medium
#210Sum Root to Leaf Numbers
medium
#113Serialize and Deserialize Binary Tree
hard
#114Binary Tree Maximum Path Sum
hard
#115Recover Binary Search Tree
hard
#116Count Complete Tree Nodes
hard
#208Binary Tree Cameras
hard