Basic Tree Problems for Coding Interviews
59 basic Tree problems from real coding interviews, each with test cases you can run online. By difficulty they split into 59 basic, 0 intermediate and 0 advanced. The companies that ask them most often are Amazon, Meta, Google, Microsoft.
Solve a few problems of each level, name the pattern after each one and revisit the ones that needed a hint a week later. Practise stating the time and space complexity and the edge cases before you code.
Coding problems
- Count Unique Binary Search Trees (Basic)
- Count Downward Paths With Target Sum (Basic)
- Tree Distance Queries (Basic)
- Count Paths With Target Sum (Basic)
- BST Range Sum (Basic)
- Find Duplicate Subtrees (Basic)
- Find All Ancestors in DAG (Basic)
- Steiner Tree Small Graph (Basic)
- Maximum Depth of Binary Tree (Basic)
- Symmetric Tree (Basic)
- Balanced Binary Tree (Basic)
- Sum of Left Leaves (Basic)
- Largest Value in Each Tree Row (Basic)
- Binary Tree Inorder Traversal (Basic)
- 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)
- Convert Sorted Array to BST (Basic)
- Binary Tree Tilt (Basic)
- Minimum Absolute Difference in BST (Basic)
- Two Sum in a BST (Basic)
- Insert Into a BST (Basic)
- Cousins in Binary Tree (Basic)
- Leaf-Similar Trees (Basic)
- Univalued Binary Tree (Basic)
- Bottom-Left Tree Value (Basic)
- Binary Tree Paths (Basic)
- Sum of Nodes With Even-Valued Grandparent (Basic)
- Level With Maximum Sum (Basic)
- Minimum Depth of Binary Tree (Basic)
- Merge Two Binary Trees (Basic)
- Sum of Root-to-Leaf Binary Numbers (Basic)
- Closest Value in a BST (Basic)
- Has Root-to-Leaf Path Sum (Basic)
- Second Minimum Node in a Binary Tree (Basic)
- Level Averages Rounded Down (Basic)
- Construct String From Binary Tree (Basic)
- Increasing Order Search Tree (Basic)
- Prune Binary Tree (Basic)
- Minimum Root-to-Leaf Path Sum (Basic)
- Nodes Without Siblings (Basic)
- Check Children Sum Property (Basic)
- Convert to Sum Tree (Basic)
- Evaluate Boolean Binary Tree (Basic)
- Is Sum Tree (Basic)
- Vertical Column Sums (Basic)
- Check if All Leaves Are at the Same Level (Basic)
- Count Subtrees With Sum Equal to X (Basic)
- Sum of Node Depths (Basic)
- Is Edge List a Tree (Basic)
- Subtree Size of Every Node (Basic)
- Leaves in Every Subtree (Basic)
- Subtree Sum Queries (Basic)
- Number of Nodes With Subtree Sum Divisible by K (Basic)
- Leaf Vertex Count (Basic)
Related
Frequently asked questions
How do I get good at tree problems?
Practise by level, name the pattern after you solve each problem and review the ones you needed hints for. The list is ordered so you can start with the basic ones.
Can I run tree problems online here?
Yes. Each problem opens in an editor that runs your code against test cases in Java, Python, C++, Go, JavaScript and more.