Skip to content
Preptima

Trees & BSTs interview questions

Traversals, recursion versus iteration, balancing, and lowest-common-ancestor problems.

3 questions in Data Structures & Algorithms.

medium3

mediumCoding

Find the lowest common ancestor of two nodes in a binary tree.

Recurse post-order and return the node whose left and right subtrees each report back one target; if only one side reports back, propagate that result upwards. This is O(n) time and O(h) stack. A BST lets you descend by value comparison instead, and parent pointers reduce it to list intersection. It also connects recursion to the point an interviewer is testing.

5 minentry, mid, senior
mediumCoding

Given a binary tree, decide whether it is a valid binary search tree.

Every node must fall inside an open interval inherited from its ancestors, not merely compare correctly against its own two children. Recurse carrying a low and high bound, tightening one of them at each descent, or walk in-order and require the sequence to be strictly increasing.

5 minentry, mid, senior