Binary Tree Maximum Path Sum
LeetCode 124 • Hard • Trees
Input: root = [-10,9,20,null,null,15,7] → Output: 42. Path 15→20→7. DFS: path_through = node + left_gain + right_gain. Return max single-branch.
TimeO(n)
SpaceO(h)
node: —
max_sum: —
Current
Path through
node—
left_gain—
right_gain—
max_sum—
Ready
Press Play. DFS post-order: path_through = node + left_gain + right_gain. Return node + max(left, right) for parent. Track global max.