Given the root of a non-empty binary tree, find the maximum path sum of any non-empty path.
A path in a binary tree:
The path sum is the sum of all node values in the path.
Constraints:
Examples:
// Example 1: // 1 // / \ // 2 3 const root1 = createTree([1, 2, 3]); console.log(maxPathSum(root1)); // Output: 6 // Explanation: Path 2 -> 1 -> 3 gives sum 2 + 1 + 3 = 6 // Example 2: // -15 // / \ // 10 20 // / \ // 15 5 // / // -5 const root2 = createTree([-15, 10, 20, null, null, 15, 5, -5]); console.log(maxPathSum(root2)); // Output: 40 // Explanation: Path 15 -> 20 -> 5 gives sum 15 + 20 + 5 = 40