Lesson overview · Free interview practice

Trees

Study tree structures and their traversal algorithms.

Topics in the full lesson
  • Concept and Use Cases
  • Types of Trees
  • When to Use
  • Time and Space Complexity
  • Tree Operations and Methods
  • Practical Tips and Tricks
  • Common Gotchas
  • Advanced Topics
  • Tree Algorithms
  • Interview Tips and Tricks
  • Common Mistakes

Try the free interview questions

Work through a question before revealing its explanation. These questions and answers are free; Premium adds the full lesson walkthrough, examples and implementation detail.

Question

Given the roots of a binary tree, find the maximum depth of the tree by traversing its nodes in a way that explores as far as possible along each branch before backtracking.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Question

Given the root of a binary tree and two nodes p and q that are present in it, return their lowest common ancestor — the deepest node that has both p and q as descendants (a node may be a descendant of itself).

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Question

Given a binary tree, write a solution to return the rightmost node at each level of the tree, representing the right side view of the tree.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Question

Implement a BSTIterator over the in-order traversal of a binary search tree. next() returns the next smallest value in the tree, and hasNext() returns whether a next value exists. The iterator starts positioned before the smallest element.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Question

Given the root of a binary tree where every node holds a single digit (0-9), each root-to-leaf path spells a number (for example the path 1 -> 2 -> 3 represents 123). Return the sum of these numbers over all root-to-leaf paths.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Explore the full Trees material

Premium includes the complete lessons and implementation references. Free practice questions remain available without a subscription.

All course tracks & premium content
From basics to advanced masterclasses
Built for JS/TS developers like you
Real-world tips & common pitfalls
Upgrade to Premium