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.

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?

In a binary tree, each node has at most two children, with no rule about their values. A binary search tree adds an ordering rule: every value in a node's left subtree is smaller than the node's value, and every value in its right subtree is larger. That ordering is what makes lookups, inserts and deletes O(log n) in a balanced BST.

What are the most common binary tree interview questions?

Common ones include checking whether a tree is balanced, validating a binary search tree, finding a tree's height, a level-order traversal, finding the lowest common ancestor of two nodes, and finding the kth smallest or second largest item in a BST.

How do you check whether a binary tree is a valid BST?

Checking each node against only its own children isn't enough, because a node deep in a left subtree could still be larger than an ancestor higher up. Instead, pass down the range of values each node is allowed to have as you traverse, or do an inorder traversal and check that the values come out in increasing order. Either way takes O(n) time.

Should I use recursion or iteration for tree problems?

Recursion usually gives the shortest, clearest code, so it's a fine default. Its call stack grows with the height of the tree, which can be O(n) for an unbalanced tree, so be ready to rewrite it with your own stack (or a queue, for a level-order traversal) if the interviewer asks.

What is the time complexity of searching a binary search tree?

O(h), where h is the height of the tree. That's O(log n) when the tree is balanced, but O(n) in the worst case, such as when values are inserted in sorted order and the tree turns into one long chain.
. . .