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

Related

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.