Binary Tree Maximum Path Sum
A path is any sequence of nodes connected by edges. Return the maximum sum of any path in the tree.
Examples
Input: [1,2,3]
Output: 6
Input: [-10,9,20,null,null,15,7]
Output: 42
Hints
Consider that a path can go through the root node, but it doesn't have to. Think about how to compute the maximum path sum that includes the current node as the highest point in the path.
For each node, calculate the maximum gain that can be obtained by including it in a path. This gain is the node's value plus the maximum of the gains from its left or right subtrees (if they contribute positively).
Maintain a global variable to track the maximum path sum encountered during the traversal. Update this variable whenever a new maximum is found, even if the path doesn't include the current node as the highest point.
Related Problems
Binary Tree Maximum Path Sum
A path is any sequence of nodes connected by edges. Return the maximum sum of any path in the tree.