Given a non-empty binary tree, return the maximum path sum. A path may start and end at any two nodes, but it must move only along parent-child connections, and it cannot reuse a node. The path does not need to pass through the root.
Input / output
root: TreeNode represented by a level-order array with null placeholdersExamples
root = [1,2,3] returns 6 for the path 2 -> 1 -> 3.root = [-10,9,20,null,null,15,7] returns 42 for the path 15 -> 20 -> 7.root = [2,-1] returns 2 because taking the root alone is best.Constraints
[1, 3 * 10^4]-1000 <= node.val <= 1000Target complexity
O(n) time with one depth-first traversal and O(h) call-stack space.Hints
Follow-up How would you modify the DFS if the interviewer asked for the actual node values on one maximum-sum path, not just the sum?