##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
You've done DP on arrays and grids. Trees add a new dimension: states live at nodes, not indices.
The Goal
The building block
Dp[node] from children
Children before parents
Knowledge check
CSES 1131
Paths pass through nodes
Deepest path down
Single DFS
Tracing the algorithm
Reconstructing the diameter
Knowledge check
LeetCode 337
Rob or skip each node
Two values per node
Return a pair
Tracing the two-state DP
Extending to more states
Knowledge check
Why rerooting?
LeetCode 834
Moving the root
Down then up
The formula
Tracing rerooting
Understanding the efficiency
Classic tree DP problem
Handling negative values
Understanding the transitions
Finding centroids
Topological peeling
Path maximum tracking
Top-down DFS pattern
Choosing the right direction
Minimum vertex cover variant
Three-state DP
Post-order with state
Why this greedy works
Designing effective states
Generalizing beyond trees
Preprocessing for fast queries
Preview of Digit DP
What we learned