Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
DATA STRUCTURES

Binary Search Trees

Trees with an ordering rule: smaller values to the left, larger to the right.

Intermediate8 problems3 interactive subtopicsO(h) time · O(1) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (3)Open in full playground →
STEP 1/3Both smaller than 6
p = 3 and q = 5 are both < 6
6
2
8
0
4
7
9
3
5
  • current
  • read by this step
  • visited / done
  • ruled out
  • result
Space Play←→ StepR Replay
1 / 3
PYTHONroot = [6, 2, 8, 0, 4, 7, 9, null, null, 3, 5], p = 3, q = 5
1def lowest_common_ancestor(root, p, q):
2 node = root
3 while node:
4 if p < node.val and q < node.val:
▶5 node = node.left
6 elif p > node.val and q > node.val:
7 node = node.right
8 else:
9 return node
STEP 1 OF 3Setup

Both smaller than 6

3 and 5 are both less than 6, so their ancestor must be in the left subtree.

VARIABLES

node
6
p
3
q
5
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

In a BST, every node’s left subtree holds smaller values and its right subtree larger ones. An inorder traversal visits the values in sorted order.

Why it matters

The ordering lets search, insert and delete skip half the tree at each step - as long as the tree stays balanced.

Think of it like this

A “higher or lower” guessing game where each answer sends you down one branch.

Use it when

  • Ordered data with frequent inserts and lookups
  • Floor, ceiling or kth-smallest queries
  • Lowest common ancestor using value comparisons

Avoid it when

  • Data arriving in sorted order with no balancing - the tree becomes a list
  • You only need membership - a hash set is simpler

Complexity cheatsheet

OperationTimeSpace
Search or insert (balanced)O(log n)O(h)
Search or insert (skewed)O(n)O(h)
Inorder traversalO(n)O(h)

Often confused with: heaps - also trees, but they only guarantee parent ≤ children, not left < right.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

BSTkth smallestvalidatelowest common ancestorinorder
  • A BST’s inorder traversal is sorted - many problems reduce to that fact
  • Validation needs a (low, high) range, not just parent comparisons
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Validating with only parent–child comparisons - deeper violations slip through
  • Ignoring duplicates - decide which side they go on
  • Assuming O(log n) without balancing

Edge cases to test

  • p or q is itself the ancestor
  • A single-node tree
  • A skewed tree built from sorted inserts
Interview tip: Mention that self-balancing trees (AVL, red-black) are what guarantee O(log n) in practice.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

Why isn’t checking node.left.val < node.val < node.right.val enough to validate a BST?

06

Practice

Apply what you just watched on real interview problems.

Generate Tree problem in AI DSA Practice →
  • BeginnerSearch in a Binary Search TreeLeetCode 700 ↗ (opens in a new tab)
  • IntermediateValidate Binary Search TreeLeetCode 98 ↗ (opens in a new tab)
  • AdvancedDelete Node in a BSTLeetCode 450 ↗ (opens in a new tab)
TreesHeaps / Priority Queues