Binary Tree and BST Interview Questions
How to approach binary tree questions
Tree questions test whether you're comfortable with recursion and with walking a tree in the right order. Expect binary trees and binary search trees (BSTs), and sometimes heaps and tries.
Most tree problems are solved with a depth-first traversal (preorder, inorder or postorder) or a breadth-first, level-by-level traversal. Work out what each node needs from its children, or what it needs to pass down to them, and the recursion usually follows. For a BST, lean on the ordering: an inorder traversal visits the values in sorted order, and you can often skip a whole subtree.
Say whether you're assuming the tree is balanced, and count the space your recursion uses. The call stack grows with the tree's height, which is O(log n) for a balanced tree but can be O(n) for a lopsided one.
Interview Cake isn't just another question database. Each question below walks you through the problem step by step, with hints and explanations as you need them, just like a real interviewer.
Binary tree and BST practice questions
Write a function to see if a binary tree is 'superbalanced'--a new tree property we just made up. keep reading »
Binary Search Tree Checker »Check whether a binary tree is a valid binary search tree. Avoid the common gotcha and solve it in O(n) time. A classic interview question, step by step. keep reading »
2nd Largest Item in a Binary Search Tree »Find the second largest element in a binary search tree. keep reading »
Menu Autocomplete »Build autocomplete for a bakery's menu search box: return every item that starts with a prefix, in sorted order, using a trie (prefix tree). keep reading »
MillionGazillion »I'm making a new search engine called MillionGazillion(tm), and I need help figuring out what data structures to use. keep reading »
Keep going with the Trees and graphs section of our coding interview course.
Concepts to review
- Binary trees: Binary tree terms and the preorder, inorder and postorder traversals.
- Binary search trees: The ordering rule, and why lookups are only O(log n) when the tree is balanced.
- Inorder traversal: Visiting a BST's values in sorted order.
- Depth-first search: Going all the way down one branch before backtracking: the basis of most recursive tree solutions.
- Breadth-first search: Visiting a tree level by level, using a queue.
- Heaps: A tree that always keeps the smallest (or largest) item at the root.
- Tries: A tree that stores strings one character at a time, for fast prefix lookups.
Quick reference: Data structures cheat sheet
Binary tree and BST interview FAQ
What's the difference between a binary tree and a binary search tree?
What are the most common binary tree interview questions?
How do you check whether a binary tree is a valid BST?
Should I use recursion or iteration for tree problems?
What is the time complexity of searching a binary search tree?
More interview questions by topic
- Array and string interview questions
- Hash table interview questions
- Greedy algorithm interview questions
- Sorting and binary search interview questions
- Graph interview questions
- Dynamic programming and recursion interview questions
- Stack and queue interview questions
- Linked list interview questions
- Bit manipulation interview questions
- System design interview questions
- All coding interview questions
Interview Cake