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.
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
- Easy01
Invert Binary Tree
Warm-up: swap each node’s children, top down.
- Easy02
Maximum Depth of Binary Tree
The one you just watched. Write it from memory.
- Easy03
Same Tree
Walk both trees together; every pair must match.
- Easy04
Subtree of Another Tree
Same-tree check, tried at every node.
- Medium05
Lowest Common Ancestor of a Binary Search Tree
Split point where the targets part ways.
- Medium06
Binary Tree Level Order Traversal
A queue walks the tree one level at a time.
- Medium07
Validate Binary Search Tree
Each node inherits a valid range from above.
- Medium08
Kth Smallest Element in a BST
In-order walk, counting until you reach k.
- Medium09
Construct Binary Tree from Preorder and Inorder Traversal
The preorder head splits the inorder list.
- Hard10
Binary Tree Maximum Path Sum
Like depth, but the best path may bend.
- Hard11
Serialize and Deserialize Binary Tree
Level order out, level order back in.