DP on Trees

You've done DP on arrays. Now states live at tree nodes. Solve Binary Tree Max Path Sum and learn how to aggregate child results at each vertex.

44 lessons
153 min
Codeforces: 1700-2200LeetCode: 1800-2200

Lessons

1. Introduction to DP on Trees

Aggregate from subtrees

2m

2. What Tree DP Solves

Diameters, paths, and matchings

3m

3. When to Use Tree DP

No cycles, natural recursion

3m

4. Problem - Binary Tree Max Path Sum

LC 124 - any path, max sum

3m1 problems

5. Binary Tree Max Path Sum - Why Naive Fails

The bend point insight

4m

6. Binary Tree Max Path Sum - Defining the DP

Return vs update: what's the difference?

4m

7. Binary Tree Max Path Sum - Transition

max(0, child) avoids negatives

4m

8. Binary Tree Max Path Sum - Base Cases

Null = 0, leaf = its value

3m

9. Binary Tree Max Path Sum - Implementation

Global update inside DFS

5m1 problems

10. Binary Tree Max Path Sum - Time and Space

O(n) time, O(h) space

3m

11. Binary Tree Max Path Sum - Edge Cases

All negative? Skewed tree?

3m

12. Lessons from Binary Tree Max Path Sum

Return one direction, update both

3m

13. Problem - Tree Distances II

CSES - distance sum per node

3m1 problems

14. Tree Distances II - Why Naive Fails

Reroot in O(1), not O(n)

4m

15. Tree Distances II - Defining the DP

Subtree size and subtree sum

4m

16. Tree Distances II - Transition

Moving root flips distances

4m

17. Tree Distances II - Base Cases

First DFS from arbitrary root

3m

18. Tree Distances II - Implementation

Two DFS passes, O(n) total

5m1 problems

19. Tree Distances II - Time and Space

O(n) beats naive O(n²)

3m

20. Tree Distances II - Edge Cases

Star graph vs path graph

3m

21. Lessons from Tree Distances II

Reroot when answer depends on root

3m

22. Problem - Distance in Tree

CF 161D - pairs at distance k

3m1 problems

23. Distance in Tree - Why Naive Fails

Combine depths across subtrees

4m

24. Distance in Tree - Defining the DP

cnt[d] = nodes at depth d

4m

25. Distance in Tree - Core Logic

Match d and k-d across children

4m

26. Distance in Tree - Implementation

Small-to-large for O(n log n)

4m1 problems

27. Lessons from Distance in Tree

Depth counting, not pair enumeration

3m

28. Problem - Tree Diameter

LC 543 - longest path in edges

3m1 problems

29. Tree Diameter - Solution

leftDepth + rightDepth through node

4m

30. Tree Diameter - Complexity

O(n) single pass

3m

31. Problem - Tree Painting

CF 1187E - maximize paint score

3m1 problems

32. Tree Painting - Solution

Sum of subtree sizes

4m

33. Tree Painting - Rerooting Formula

score[v] = score[u] + n - 2*size[v]

3m

34. Problem - Tree Matching

Maximum matching in tree

3m1 problems

35. Tree Matching - Solution

dp[v][matched] or dp[v][unmatched]

4m

36. Tree Matching - Greedy Solution

Greedy from leaves up

3m

37. Problem - Binary Tree Cameras

LC 968 - minimum cameras

4m1 problems

38. Binary Tree Cameras - Solution

Three states: uncovered, covered, has camera

4m1 problems

39. Quiz: Tree DP Patterns

Which subtree pattern is this?

3m1 problems

40. Quiz: Tree DP Edge Cases

Single node, empty tree

3m1 problems

41. Common Mistakes in Tree DP

Post-order for bottom-up

4m

42. Problem - Longest Path With Different Adjacent Characters

LC 2246

3m1 problems

43. Longest Path With Different Adjacent Characters - Implementation

Solution approach

5m1 problems

44. Section Recap

From path sum to rerooting

3m

Practice Problems

Ready to start learning?

Access all 44 lessons with interactive content and progress tracking.