Tree Problems for Coding Interviews
Tree problems reward recursion: define what a function returns for one node assuming its children are solved, then combine. Depth-first traversals (pre-order, in-order, post-order) and breadth-first level order cover most questions.
Know the properties of a binary search tree, how to compute height, diameter and lowest common ancestor, and how to serialise a tree. Practise writing both the recursive and the iterative version and stating the space cost of the call stack.
Coding problems
- Count Unique Binary Search Trees (Basic)
- Minimum Height Trees (Intermediate)
- Minimum Edge Reversals Per Node (Intermediate)
- Count Downward Paths With Target Sum (Basic)
- Lowest Common Ancestor With Parent Pointers (Intermediate)
- Tree Infection Time (Intermediate)
- Tree Nodes at Distance K (Advanced)
- Maximum Weight Independent Set on Tree (Advanced)
- Minimum Dominating Set on Tree (Advanced)
- Tree Distance Queries (Basic)
- Tree Diameter (Advanced)
- Tree Vertical Traversal (Intermediate)
- Count Paths With Target Sum (Basic)
- BST Range Sum (Basic)
- Dynamic Tree Value Updates (Intermediate)
- BST Successor and Predecessor (Intermediate)
- Find Duplicate Subtrees (Basic)
- Root-to-Leaf Number Sum (Intermediate)
- Tree Path With Maximum Average (Intermediate)
- N-ary Tree Diameter (Advanced)
- Find All Ancestors in DAG (Basic)
- Steiner Tree Small Graph (Basic)
- Maximum Depth of Binary Tree (Basic)
- Symmetric Tree (Basic)
- Balanced Binary Tree (Basic)
- Validate Binary Search Tree (Intermediate)
- Kth Smallest Value in a BST (Intermediate)
- Binary Tree Maximum Path Sum (Advanced)
- Count Good Nodes in Binary Tree (Intermediate)
- Sum of Left Leaves (Basic)
- Binary Tree Right Side View (Intermediate)
- Largest Value in Each Tree Row (Basic)
- Zigzag Level Order Traversal (Intermediate)
- Binary Tree Inorder Traversal (Basic)
- Lowest Common Ancestor of Two Values (Intermediate)
- Deepest Leaves Sum (Intermediate)
- Longest Univalue Path (Intermediate)
- Maximum Ancestor Difference (Intermediate)
- Subtree of Another Tree (Intermediate)
- Boundary of Binary Tree (Advanced)
- Pseudo-Palindromic Root-to-Leaf Paths (Intermediate)
- Same Tree (Basic)
- Invert Binary Tree (Basic)
- Binary Tree Preorder Traversal (Basic)
- Binary Tree Postorder Traversal (Basic)
- Level Order Traversal (Basic)
- Bottom-Up Level Order (Basic)
- Level Sums of Binary Tree (Basic)
- Count Complete Tree Nodes (Intermediate)
- Convert Sorted Array to BST (Basic)
- Tree From Preorder and Inorder (Intermediate)
- Tree From Inorder and Postorder (Intermediate)
- Binary Tree Tilt (Basic)
- Mode Values in a BST (Intermediate)
- Minimum Absolute Difference in BST (Basic)
- Two Sum in a BST (Basic)
- Trim a BST (Intermediate)
- Insert Into a BST (Basic)
- Delete Node in a BST (Intermediate)
- Recover Swapped BST Values (Intermediate)
Related
- Sliding window problems
- Dynamic programming problems
- Graph problems
- Practice coding problems online
- All interview question topics
Frequently asked questions
How do I get good at tree problems?
Solve a few problems of each difficulty, name the pattern after you solve each one, and revisit the ones you needed hints for a week later. The list on this page is ordered so you can start with the basic ones.
Can I run tree problems online here?
Yes. Every problem opens in an editor that runs your code against test cases in Java, Python, C++, Go, JavaScript and six more languages, with no setup.