Dynamic Programming21 sections · 916 units
Open in Course

The Tree DP Pattern

Dp[node] from children

Here's the core pattern: dp[v]dp[v] = some combination of dp[child]dp[\text{child}] for all children of vv. For leaves (nodes with no children), dp[leaf]dp[\text{leaf}] comes directly from the node's properties.

For internal nodes, you combine children's dp values according to the problem's logic. The answer is usually dp[root]dp[\text{root}], representing the solution for the entire tree. Sometimes you track a global maximum across all nodes instead.