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)

2 · Watch

No sound? Captions are on by default.

3 · Now you drive

Keys:←→ stepSpace playR reset

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▶ marks the line running now
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);
}

4 · Your turn

0 of 11 problems solved
  1. Easy01

    Invert Binary Tree

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

  2. Easy02

    Maximum Depth of Binary Tree

    The one you just watched. Write it from memory.

  3. Easy03

    Same Tree

    Walk both trees together; every pair must match.

  4. Easy04

    Subtree of Another Tree

    Same-tree check, tried at every node.

  5. Medium05

    Lowest Common Ancestor of a Binary Search Tree

    Split point where the targets part ways.

  6. Medium06

    Binary Tree Level Order Traversal

    A queue walks the tree one level at a time.

  7. Medium07

    Validate Binary Search Tree

    Each node inherits a valid range from above.

  8. Medium08

    Kth Smallest Element in a BST

    In-order walk, counting until you reach k.

  9. Medium09

    Construct Binary Tree from Preorder and Inorder Traversal

    The preorder head splits the inorder list.

  10. Hard10

    Binary Tree Maximum Path Sum

    Like depth, but the best path may bend.

  11. Hard11

    Serialize and Deserialize Binary Tree

    Level order out, level order back in.

UP NEXT · LESSON 8 OF 15TriesShare prefixes, find words letter by letter.