Stage 1 · Code
Trees
Lowest Common Ancestor
LCA in BST and binary trees with parent pointers.
LCA in BST
In BST, use property: if both nodes < current, LCA in left; if both > current, LCA in right; otherwise current is LCA.
LCA in Binary Tree
Recursive: if root is p or q, return root. Recurse left/right. If both non-nil, root is LCA. If one non-nil, return that (found p or q in subtree).
LCA with Parent Pointers
If nodes have parent pointers, find LCA by climbing up. Method 1: HashSet of p's ancestors, climb q. Method 2: Two pointers, switch to other's start when reaching root (like intersection of linked lists).
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.