When to Use Tree DP

No cycles, natural recursion

These are the signs of Tree DP problems:

1.1. Definitely, when we have tree structure. Vertices are connected without cycles.

2.2. Aggregate computation. Answers depend on combining subtree results.

3.3. Path or subtree properties. Maximum path, counting configurations, distance sums.

The state is typically dp[v]dp[v] representing the answer for the subtree rooted at vv. For re-rooting problems, you'll have dp[v]dp[v] when vv is the root.

unnamed (98).jpg