Trees & BST problems, grouped by pattern
Trees are recursion with structure. Traversals come first, then postorder with multiple return values, LCA, views, serialization and the BST property. Most tree problems reduce to deciding what each call returns to its parent.
When to reach for it
- The input is a root node
- You need a value per level, per path or per subtree
- The tree is a BST and order matters
Mistakes to watch for
- Validating a BST using only the parent value
- Mixing up what a node returns vs what it records globally
- Forgetting null children in serialization
The 71 problems
1Recursion
- Binary Tree Inorder Traversal (Recursive) Animated Easy
- Binary Tree Preorder Traversal (Recursive) Animated Easy
- Binary Tree Postorder Traversal (Recursive) Animated Easy
- Binary Tree Inorder Traversal (Iterative) Animated Easy
- Binary Tree Preorder Traversal (Iterative) Animated Easy
- Binary Tree Postorder Traversal (Iterative) Animated Easy
- Maximum Depth of Binary Tree Animated Easy
- Same Tree Animated Easy
- Symmetric Tree Animated Easy
- Invert Binary Tree Animated Easy
- Binary Tree Upside Down Medium
- Second Minimum Node In a Binary Tree Easy
- Sum Root to Leaf Numbers Medium
2Backtracking
- Root to Leaf Paths Animated Medium
- Path Sum II Medium
- Path Sum III Medium
3Traversals
- Binary Tree Level Order Traversal Animated Medium
- Binary Tree Zigzag Level Order Traversal Animated Medium
- Binary Tree Level Order Traversal II Animated Medium
- Populating Next Right Pointers in Each Node Medium
4Views
- Top View of Binary Tree Animated Medium
- Bottom View of Binary Tree Animated Medium
- Left View of Binary Tree Animated Easy
- Binary Tree Right Side View Animated Medium
- Binary Tree Vertical Order Traversal Medium
- Vertical Order Traversal of a Binary Tree Hard
- Boundary of Binary Tree Medium
5BST Basics
- Search in a Binary Search Tree Animated Easy
- Minimum element in BST Animated Easy
- Insert into a Binary Search Tree Animated Medium
- Binary Search Tree Iterator Medium
- Convert Sorted Array to Binary Search Tree Easy
- Balance a Binary Search Tree Medium
- Delete Node in a BST Medium
- Range Sum of BST Easy
- Recover Binary Search Tree Medium
- Closest Binary Search Tree Value II Hard
6Postorder + Multiple Return Values
- Balanced Binary Tree Animated Easy
- Validate Binary Search Tree Animated Medium
- Children Sum in a Binary Tree Animated Easy
- Largest BST Animated Medium
- Binary Tree Maximum Path Sum Hard
- Diameter of Binary Tree Easy
- Distribute Coins in Binary Tree Medium
- Find Leaves of Binary Tree Medium
- House Robber III Medium
- Count Nodes With the Highest Score Medium
- Delete Nodes And Return Forest Medium
7LCA
- Lowest Common Ancestor of a Binary Tree Animated Medium
- Lowest Common Ancestor of a Binary Search Tree Animated Medium
- Smallest Common Region Medium
- Lowest Common Ancestor of a Binary Tree IV Medium
- Step-By-Step Directions From a Binary Tree Node to Another Medium
8Child + Ancestor Handling
- Amount of Time for Binary Tree to Be Infected Animated Medium
- Burning Tree Animated Hard
- All Nodes Distance K in Binary Tree Animated Medium
- Count Good Nodes in Binary Tree Medium
9Reverse Inorder Traversal
- Kth Smallest Element in a BST Animated Medium
- Kth Largest Element in BST Animated Easy
10Tree Serialization / Deserialization
- Construct Binary Tree from Preorder and Inorder Traversal Animated Medium
- Construct Binary Tree from Inorder and Postorder Traversal Animated Medium
- Construct Binary Search Tree from Preorder Traversal Animated Medium
- Serialize and Deserialize Binary Tree Animated Hard
- Serialize and Deserialize N-ary Tree Hard
11Tree to Lists and vice-versa
- Binary Tree to DLL Animated Hard
- Flatten Binary Tree to Linked List Animated Medium
12Miscellaneous BST Questions
- Inorder Successor in BST Animated Medium
- Inorder predecessor Animated Medium
- Floor in BST Animated Easy
- Ceil in BST Animated Easy
- Two Sum IV - Input is a BST Animated Easy