Subtree DP

Processing trees from leaves to root. Aggregating subtree values. Include/exclude patterns and path optimization.

44 lessons
135 min

Lessons

1. Intro

The Goal

2m

2. Trees as Recursive Structures

Each subtree is a tree

3m

3. What is Subtree DP

Bottom-up computation on trees

3m

4. DFS for Tree DP

Post-order traversal pattern

3m

5. Example: Count Subtree Sizes

Simplest aggregation problem

3m

6. Quiz: Basic Tree DP

Knowledge check

2m1 problems

7. State Definition on Trees

What does dp[node] represent

3m

8. Aggregating Child Values

Sum, max, count patterns

3m

9. Time Complexity of Tree DP

Linear in node count

3m

10. Rooted vs Unrooted Trees

Picking a root matters

3m

11. CSES 1674 Subordinates - Problem Statement

CSES 1674

2m1 problems

12. CSES 1674 Subordinates - Core Idea

Subtree size minus one

3m

13. CSES 1674 Subordinates - Recurrence

Sum of child subtrees

3m

14. CSES 1674 Subordinates - Algorithm

Single DFS pass

4m

15. CSES 1674 Subordinates - Implementation

Code with adjacency list

3m1 problems

16. CSES 1674 Subordinates - Walkthrough

Example tree trace

4m

17. CSES 1674 Subordinates - Lessons

Subtree aggregation

3m

18. Quiz: Subordinates Problem

Knowledge check

2m1 problems

19. Include/Exclude Pattern

Two states per node

3m

20. Include/Exclude Transitions

Combining child states

4m

21. LeetCode 337 House Robber III - Problem Statement

LeetCode 337

2m1 problems

22. LeetCode 337 House Robber III - State Definition

Two values per node

3m

23. LeetCode 337 House Robber III - Recurrence

Include or exclude logic

4m

24. LeetCode 337 House Robber III - Base Case

Null nodes return zero

3m

25. LeetCode 337 House Robber III - Implementation

DFS returning pair

4m1 problems

26. LeetCode 337 House Robber III - Walkthrough

Example tree trace

4m

27. LeetCode 337 House Robber III - Lessons

Include/exclude on trees

3m

28. Quiz: Include/Exclude Pattern

Knowledge check

2m1 problems

29. Path Problems on Trees

Paths through nodes

4m

30. Combining Paths from Children

Pick two best branches

3m

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

LeetCode 124

2m1 problems

32. LeetCode 124 Binary Tree Maximum Path Sum - State Definition

Max downward path from node

3m

33. LeetCode 124 Binary Tree Maximum Path Sum - Core Idea

Path through node vs from node

4m

34. LeetCode 124 Binary Tree Maximum Path Sum - Handling Negatives

Skip negative branches

3m

35. LeetCode 124 Binary Tree Maximum Path Sum - Algorithm

DFS with global max

4m

36. LeetCode 124 Binary Tree Maximum Path Sum - Implementation

Code with global variable

4m1 problems

37. LeetCode 124 Binary Tree Maximum Path Sum - Walkthrough

Example tree trace

4m

38. LeetCode 124 Binary Tree Maximum Path Sum - Lessons

Two values pattern

3m

39. Quiz: Path Problems

Knowledge check

2m1 problems

40. Rerooting Technique Preview

Computing for all roots

3m

41. Common Mistakes in Tree DP

What breaks your solution

3m

42. Tree DP vs Graph DP

Why trees are simpler

3m

43. When to Use Subtree DP

Problem recognition patterns

3m

44. Section Recap

What we learned

3m

Practice Problems

1.
House Robber IIILeetCodemedium
3.
Tree MatchingCSESmedium
4.
Appleman and TreeCodeforceshard
5.
Book of EvilCodeforcesmedium
6.
Distance in TreeCodeforceshard

Ready to start learning?

Access all 44 lessons with interactive content and progress tracking.