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

Trees

Hierarchies of nodes - and the ways to walk them.

Intermediate12 problems5 interactive subtopicsO(n) time · O(h) stack
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (5)Open in full playground →
STEP 1/4Start at 3
start at the root
3
9
20
15
7
  • current
  • not reached yet
  • result
Space Play←→ StepR Replay
1 / 4
PYTHONroot = [3, 9, 20, null, null, 15, 7]
1def max_depth(root):
2 if root is None:
3 return 0
▶4 left = max_depth(root.left)
5 right = max_depth(root.right)
6 return 1 + max(left, right)
STEP 1 OF 4Setup

Start at 3

3 isn’t empty, so recurse into the left child first.

VARIABLES

node
3
left
-
right
-
02

Intuition & complexity

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

What it is

A binary tree node has a value and up to two children. Traversals visit nodes depth-first (preorder, inorder, postorder) or level by level.

Why it matters

File systems, HTML, org charts and expression parsers are trees, and recursion handles them naturally.

Think of it like this

A family tree: you can explore one branch fully before moving to the next.

Use it when

  • Hierarchical data
  • Answers that combine results from the left and right subtrees
  • Level-based questions (BFS)

Avoid it when

  • Flat sequential data - arrays are simpler
  • Very deep, skewed trees with plain recursion in Python

Complexity cheatsheet

OperationTimeSpace
Any depth-first traversalO(n)O(h) stack
Level order with a queueO(n)O(w) for width w
Find a value (no ordering)O(n)O(h)

Often confused with: binary search trees - they add an ordering rule that general binary trees don’t have.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

rootTreeNodedepthsubtreelevel order
  • Most tree answers are “solve left, solve right, combine”
  • Recursion depth equals tree height - O(log n) if balanced, O(n) if skewed
04

Mistakes & edge cases

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

Common mistakes

  • Forgetting the None base case
  • Mixing up depth counted in edges vs nodes - ask which the prompt means
  • Using a global variable without resetting it between test cases

Edge cases to test

  • Empty tree (root is None)
  • A single node
  • A skewed tree - like a linked list, depth n
Interview tip: Name the traversal and why: inorder for a BST’s sorted order, postorder when a parent needs its children’s answers.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

Which traversal visits a node after both of its subtrees?

06

Practice

Apply what you just watched on real interview problems.

Generate Tree problem in AI DSA Practice →
  • BeginnerMaximum Depth of Binary TreeLeetCode 104 ↗ (opens in a new tab)
  • IntermediateBinary Tree Level Order TraversalLeetCode 102 ↗ (opens in a new tab)
  • AdvancedBinary Tree Maximum Path SumLeetCode 124 ↗ (opens in a new tab)
Hash MapsBinary Search Trees