Skip to content

Start typing to search patterns, problems and pages.

Trees

A family tree spans as many generations as its longest branch. Each person asks their children how far down their lines run, then adds one for themselves. The root learns the whole height without ever seeing it.

⏱ about 15 minutesNeeds: recursionTime O(n)Space O(h)

2 · Watch

Trees in 60 seconds

A tree with five nodes, and you need its depth. Walking every root-to-leaf path visits the shared nodes again and again.

3 · Now you drive

Keys:←→ stepSpace playR resetHomeEnd jump

Maximum Depth of Binary Tree

or try
Level order, like LeetCode: 3 9 20 null null 15 7. Up to 4 levels.
Ask the root, 3, how deep its tree goes.Step 1 of 11. At 3, depth 1. Call stack: 3. Asking how deep it goes.
Step 1 of 11
solution.ts
function maxDepth(root: TreeNode | null): number {
if (root === null) return 0;
const left = maxDepth(root.left);
const right = maxDepth(root.right);
return 1 + Math.max(left, right);
}
▶ marks the line running now

4 · Your turn

0 of 11 problems solved

Start from this

function dfs(node: TreeNode | null): number {
  // Empty node: the smallest case, answer it directly.
  if (!node) return 0;
  // Trust each call to solve its own subtree.
  const left = dfs(node.left);
  const right = dfs(node.right);
  // Build this node's answer from its children's.
  return combine(node, left, right);
}

Easy ways to go wrong

  • Leaving out the empty-node base case.
  • Checking a node against its parent only. BST rules cover the whole subtree.
  • Mixing up what a call returns with the answer you’re tracking.
  1. Easy01

    Invert Binary Tree

    LeetCode 226 · Blind 75 · NeetCode 150

    Warm-up: swap each node’s children, top down.

  2. Easy02

    Maximum Depth of Binary Tree

    LeetCode 104 · Blind 75 · NeetCode 150

    The one you just watched. Write it from memory.

  3. Easy03

    Same Tree

    LeetCode 100 · Blind 75 · NeetCode 150

    Walk both trees together; every pair must match.

  4. Easy04

    Subtree of Another Tree

    LeetCode 572 · Blind 75 · NeetCode 150

    Same-tree check, tried at every node.

  5. Medium05

    Lowest Common Ancestor of a Binary Search Tree

    LeetCode 235 · Blind 75 · NeetCode 150

    Split point where the targets part ways.

  6. Medium06

    Binary Tree Level Order Traversal

    LeetCode 102 · Blind 75 · NeetCode 150

    A queue walks the tree one level at a time.

  7. Medium07

    Validate Binary Search Tree

    LeetCode 98 · Blind 75 · NeetCode 150

    Each node inherits a valid range from above.

  8. Medium08

    Kth Smallest Element in a BST

    LeetCode 230 · Blind 75 · NeetCode 150

    In-order walk, counting until you reach k.

  9. Medium09

    Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 105 · Blind 75 · NeetCode 150

    The preorder head splits the inorder list.

  10. Hard10

    Binary Tree Maximum Path Sum

    LeetCode 124 · Blind 75 · NeetCode 150

    Like depth, but the best path may bend.

  11. Hard11

    Serialize and Deserialize Binary Tree

    LeetCode 297 · Blind 75 · NeetCode 150

    Level order out, level order back in.

UP NEXT · LESSON 10 OF 20TriesShare prefixes, find words letter by letter.