Stage 1 · Code
Dynamic Programming
Tree DP
DP on trees: diameter, max path sum, house robber III.
Tree DP Concept
DP on trees: for each node, compute answer using children's results. Typically post-order traversal (process children before parent). Return value(s) to parent. Two patterns: (1) single value returned, (2) pair of values (e.g., take/skip).
Tree Diameter
Longest path between any two nodes. For each node, compute longest path through it = max depth of left + max depth of right. Track global max.
Max Path Sum
Path can start/end at any nodes, must be connected. For each node, compute max path sum starting at node and going down. Also track max path through node (left + node + right).
House Robber III
Binary tree, can't rob adjacent nodes. For each node, return pair: (rob this node, skip this node). If rob: node.Val + skip(left) + skip(right). If skip: max(rob, skip) for both children.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.