Lowest Common Ancestor (LCA)

You've learned binary lifting. Now find the lowest common ancestor of two nodes efficiently.

51 lessons
152 min

Lessons

1. Intro

Finding ancestors fast

3m

2. What is LCA?

(Nearest shared ancestor)

3m

3. Why LCA Matters

(Distance and path queries)

3m

4. Naive Approach - The Idea

(Climb to same depth)

3m

5. Naive Approach - Implementation

(Climbing step by step)

3m

6. Binary Lifting for LCA

(Jump in powers of two)

3m

7. Binary Lifting - Preprocessing

(Building the up table)

3m

8. Core Idea - Binary Search on Ancestors

(Finding the split point)

3m

9. Binary Lifting - Climbing Together

(Find the meeting point)

3m

10. Binary Lifting - Complexity

(Preprocessing and query time)

3m

11. Problem - LCA of a Binary Tree

(LeetCode 236)

3m1 problems

12. Problem - LCA of a Binary Tree - Read Statement

(LeetCode 236)

3m1 problems

13. Core Idea - Multiple Approaches Exist

(enabling array algorithms)

3m

14. Core Idea - Base Cases

(Null and target nodes)

3m

15. Algorithm - Recursive LCA

Combine left and right results

3m

16. Implementation - Recursive LCA

(Pseudocode)

3m

17. Implementation - LCA of a Binary Tree - Implement Solution

(LeetCode 236)

3m1 problems

18. Walkthrough - Example Tree

(Tracing the recursion)

3m

19. Lessons - Recursive Tree Traversal

(Bottom-up propagation)

3m

20. Euler Tour for LCA

(Convert tree to array)

3m

21. Euler Tour - Construction

(DFS with recording)

3m

22. LCA to RMQ Reduction

(Minimum depth in range)

3m

23. Sparse Table for RMQ

(Precompute range minimums)

3m

24. Sparse Table - Building

(Dynamic programming)

3m

25. Sparse Table - Querying

(Overlapping ranges trick)

3m

26. Euler Tour + RMQ - Full Algorithm

(Combining both techniques)

3m

27. Euler Tour + RMQ - Complexity

(Time and space analysis)

3m

28. Distance Between Two Nodes

(Using LCA)

3m

29. Problem - Distance Queries

(CSES 1135)

3m1 problems

30. Problem - Distance Queries - Read Statement

(CSES 1135)

3m1 problems

31. Core Idea - Precompute Depths and LCA

(Setup for fast queries)

3m

32. Implementation - Distance Queries

(Pseudocode)

3m1 problems

33. Implementation - Distance Queries - Implement Solution

(CSES 1135)

3m1 problems

34. Lessons - Amortized Preprocessing

(Pay once, query many)

3m

35. Problem - Counting Paths

(CSES 1136)

3m1 problems

36. Problem - Counting Paths - Read Statement

(CSES 1136)

3m1 problems

37. Core Idea - Path as Range Update

Add to endpoints, subtract ...

3m

38. Core Idea - Propagating Counts

(Sum children contributions)

3m

39. Algorithm - Counting Paths

(Mark and propagate)

3m

40. Implementation - Counting Paths

(Pseudocode)

3m1 problems

41. Implementation - Counting Paths - Implement Solution

(CSES 1136)

3m1 problems

42. Walkthrough - Example

(Tracing the algorithm)

3m

43. Core Insight - Parent Decrement

(Fixing the formula)

3m

44. Lessons - Difference Arrays on Trees

(Efficient path updates)

3m

45. Path Queries Using LCA

(Maximum, minimum, sum)

3m

46. Weighted Binary Lifting

(Carrying extra data)

3m

47. Quiz: LCA Distance

Knowledge check

2m1 problems

48. Binary Lifting vs Euler Tour

(When to use each)

3m

49. Common Mistakes

(Pitfalls to avoid)

3m

50. Extensions

(Beyond basic LCA)

3m

51. Section Recap

(What you learned)

3m

Practice Problems

1.
LCA of Binary TreeLeetCodemedium
2.
3.

Ready to start learning?

Access all 51 lessons with interactive content and progress tracking.