DP on Trees

You've done DP on arrays and grids. Trees add a new dimension: states live at nodes, not indices.

43 lessons
193 min
Codeforces: 1600-2400LeetCode: 1800-2300

Lessons

1. Intro

The Goal

5m

2. Vocabulary - Subtree

The building block

5m

3. The Tree DP Pattern

Dp[node] from children

6m

4. Why Post-Order?

Children before parents

5m

5. Quiz: Tree DP Basics

Knowledge check

3m1 problems

6. Tree Diameter - Problem Statement

CSES 1131

5m1 problems

7. Tree Diameter - Key Observation

Paths pass through nodes

5m

8. Tree Diameter - State Design

Deepest path down

5m

9. Tree Diameter - Implementation

Single DFS

5m1 problems

10. Tree Diameter - Walkthrough

Tracing the algorithm

4m

11. Challenge: Finding the Actual Path

Reconstructing the diameter

4m

12. Quiz: Tree Diameter

Knowledge check

3m1 problems

13. LeetCode 337 House Robber III - Problem Statement

LeetCode 337

5m1 problems

14. LeetCode 337 House Robber III - Key Observation

Rob or skip each node

5m

15. LeetCode 337 House Robber III - State Design

Two values per node

4m

16. LeetCode 337 House Robber III - Implementation

Return a pair

5m1 problems

17. LeetCode 337 House Robber III - Walkthrough

Tracing the two-state DP

4m

18. Challenge: Three States

Extending to more states

4m

19. Quiz: House Robber III

Knowledge check

3m1 problems

20. Rerooting Technique

Why rerooting?

6m

21. LeetCode 834 Sum of Distances in Tree - Problem Statement

LeetCode 834

5m1 problems

22. LeetCode 834 Sum of Distances in Tree - Key Observation

Moving the root

8m

23. LeetCode 834 Sum of Distances in Tree - Two Passes

Down then up

6m

24. LeetCode 834 Sum of Distances in Tree - Implementation

The formula

5m1 problems

25. LeetCode 834 Sum of Distances in Tree - Walkthrough

Tracing rerooting

4m

26. Quiz: Rerooting Complexity

Understanding the efficiency

3m1 problems

27. LeetCode 124 Binary Tree Maximum Path Sum - Problem Statement

Classic tree DP problem

3m

28. LeetCode 124 Binary Tree Maximum Path Sum - Implementation

Handling negative values

5m

29. Quiz: Maximum Path Sum

Understanding the transitions

3m1 problems

30. LeetCode 310 Minimum Height Trees - Problem Statement

Finding centroids

3m

31. LeetCode 310 Minimum Height Trees - Implementation

Topological peeling

5m

32. LeetCode 1448 Count Good Nodes in Binary Tree - Problem Statement

Path maximum tracking

3m

33. LeetCode 1448 Count Good Nodes in Binary Tree - Implementation

Top-down DFS pattern

5m

34. Quiz: Top-Down vs Bottom-Up

Choosing the right direction

3m1 problems

35. LeetCode 968 Binary Tree Cameras - Problem Statement

Minimum vertex cover variant

3m

36. LeetCode 968 Binary Tree Cameras - State Transitions

Three-state DP

5m

37. LeetCode 968 Binary Tree Cameras - Implementation

Post-order with state

5m

38. Quiz: Tree Cameras Intuition

Why this greedy works

3m1 problems

39. Pattern - Tree DP State Design

Designing effective states

4m

40. Challenge: DP on DAGs

Generalizing beyond trees

4m

41. Challenge: Tree DP with Queries

Preprocessing for fast queries

4m

42. What's Next

Preview of Digit DP

4m

43. Section Recap

What we learned

9m

Practice Problems

1.
House Robber IIILeetCodemedium

Perfect intro to tree DP state propagation

2.
Maximum White SubtreeCodeforceshard

Classic rerooting technique for all nodes

3.

Covered with full walkthrough in this section.

4.

Covered with full walkthrough in this section.

5.

Classic rerooting DP. Find node minimizing edge reversals to reach all others.

6.
Distance in TreeCodeforcesmedium

Count vertex pairs at distance k. Fundamental tree DP with path counting.

7.
Tree PaintingCodeforceshard

Maximize sum of subtree sizes during traversal. Beautiful rerooting application.

8.

Maximize weighted distance sum from chosen root. Rerooting with contribution analysis.

9.
Three Paths on a TreeCodeforceshard

Choose 3 nodes maximizing covered edges. Tree diameter extension problem.

10.

Max path value with node values and edge costs. Tree path DP with pruning.

11.

Max sum path in binary tree. Classic interview problem teaching path vs subtree distinction.

12.

Minimum cameras to monitor all nodes. Tree DP with 3 states per node.

13.

Longest path with constraint on adjacent nodes. Tree DP combining two best children.

14.
Tree DiameterCodeforcesmedium

Find diameter using interactive queries. Tree structure deduction.

15.
Same Sum BlocksCodeforcesmedium

Find max non-overlapping segments with equal sums. Greedy + interval selection.

16.

Choose k components maximizing average sum. Tree DP with greedy selection.

17.

Count independent sets on a tree - foundational tree DP with take/skip pattern.

18.

Paint subtrees black with constraints - teaches rerooting technique for all-nodes answers.

Ready to start learning?

Access all 43 lessons with interactive content and progress tracking.