A path is any sequence of nodes connected by edges (it need not pass through the root and can start/end anywhere). Return the maximum node-value sum over all paths. e.g. [-10,9,20,null,null,15,7] → 42 (15+20+7).
Separating two different quantities in one postorder DFS: what a subtree can CONTRIBUTE upward (a single downward arm) versus the best path that can PEAK at a node (both arms).
The hard part is that a node participates in the answer in two incompatible ways, and you must return one while recording the other. Do a postorder DFS. For a node, recursively get the best downward gain from each child, clamped at 0 (drop a negative arm). The best path that PEAKS at this node — allowed to bend through it using both children — is node.val + leftGain + rightGain; update a global maximum with it. But the value you RETURN to the parent can extend only one arm (a parent can't route through both of your children), so return node.val + max(leftGain, rightGain). Keeping these two computations distinct is the entire trick. Recognition signal: a tree problem where the global optimum can BEND at a node but the value propagated to the parent is a single path → postorder DFS returning the one-arm gain while side-updating a global best with the two-arm peak. O(n) time, O(h) stack.
Tree optima where a node may join two subtree results for the answer but can pass only one upward → postorder DFS: return the best single arm, update a global with the both-arms combination.
Time O(n) · Space O(h)