A max path either passes through a node using both sides, or continues upward using only one side.
DFS returns the best “gain” from a node to its parent: node.val + max(0, leftGain, rightGain).
Update a global answer with a path that uses both sides: node.val + max(0,left) + max(0,right).
Every maximum path has a highest node where the path turns (may include both children). Evaluating each node as that turn covers all paths.
A max path either passes through a node using both sides, or continues upward using only one side.
DFS returns the best “gain” from a node to its parent: node.val + max(0, leftGain, rightGain).
Update a global answer with a path that uses both sides: node.val + max(0,left) + max(0,right).
Every maximum path has a highest node where the path turns (may include both children). Evaluating each node as that turn covers all paths.