##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
Processing trees from leaves to root. Aggregating subtree values. Include/exclude patterns and path optimization.
The Goal
Each subtree is a tree
Bottom-up computation on trees
Post-order traversal pattern
Simplest aggregation problem
Knowledge check
What does dp[node] represent
Sum, max, count patterns
Linear in node count
Picking a root matters
CSES 1674
Subtree size minus one
Sum of child subtrees
Single DFS pass
Code with adjacency list
Example tree trace
Subtree aggregation
Knowledge check
Two states per node
Combining child states
LeetCode 337
Two values per node
Include or exclude logic
Null nodes return zero
DFS returning pair
Example tree trace
Include/exclude on trees
Knowledge check
Paths through nodes
Pick two best branches
LeetCode 124
Max downward path from node
Path through node vs from node
Skip negative branches
DFS with global max
Code with global variable
Example tree trace
Two values pattern
Knowledge check
Computing for all roots
What breaks your solution
Why trees are simpler
Problem recognition patterns
What we learned