Trees interview questions
Binary tree recursion, traversal orders, level-by-level walks, binary search tree rules and the questions built on them, all with one small Node class.
– interview-ready · 16 questions in this topic
– of 16 tried
Saved on this device. Interview-ready means you got it right on two different days. Guesses and hints count for less.
Questions
- How trees are written on these pagesPredict
- How deep is the deepest branch?Pick the fix
- Is this a valid binary search tree?Pick the fix
- List a tree's values row by rowFill the blank
- Are two trees identical?Predict
- Does a tree mirror itself?Pick the fix
- Swap every node's left and right childrenPredict
- Longest path between any two nodesPick the fix
- Is every node's left and right height within one?Predict
- What you see from the right of a treePick the fix
- The k-th smallest value in a search treeOrder the steps
- Closest shared ancestor in a search treePredict
- Does a root-to-leaf route add up to a target?Pick the fix
- Pre-order, in-order, post-order and level orderPredict
- Why search trees need to stay balancedPredict
- Recursion depth and when to walk a tree iterativelyPredict
Practise this topic
Flashcards · 26
Flip, then say if you knew it. Needs JavaScript; every answer is also on its question page.
Quick quiz · 16 questions
One exercise per question, checked as you go. Needs JavaScript.
Mock interview · 3 sets
Timed, out loud, then tick what you covered. Needs JavaScript.
Quick facts
Recall the answer first, then tap Show to check.
@dataclass@dataclasswrites__init__,__repr__and__eq__from the annotated fields; a tree node needs three fields and no methods.Python recursion limit
CPython's default recursion limit is 1000 (
sys.getrecursionlimit()); a chain-shaped tree deeper than that needs an iterative walk.Kotlin nullable children
var left: Node? = nullmakes a missing child explicit;node?.leftsafely returns null whennodeis null.Heap-style indexes
Storing a tree in a list with children of index i at 2i + 1 and 2i + 2 is how this course writes test trees, and how binary heaps are stored.
collections.dequefor BFSBFS appends to the right and pops from the left; a deque does both in O(1), a list's
pop(0)is O(n).
Printable cheat sheet · Review your weak spots in this topic
Share your progress
Sources: Topic outline informed by the Tech Interview Handbook by Yangshun Tay and the NeetCode 150 pattern grouping by neetcode-gh, both MIT licensed. Every problem, solution and explanation is written by Best Latest Apps. Credits and license