Largest path sum in a tree
A path is any sequence of nodes where each is a parent or child of the next, with no node
repeated; it need not pass through the root or end at a leaf. Write max_path_sum(root)
that returns the largest sum of values along any path in a non-empty binary tree.
max_path_sum([1, 2, 3]) -> 6
max_path_sum([-10, 9, 20, None, None, 15, 7]) -> 42 (15 + 20 + 7)
max_path_sum([-3]) -> -3
At each node, the best path through it is val + best downward left + best downward right, where a negative downward contribution is replaced by zero. The function returns
the best downward path (one side only) to its parent while updating a global best.
def max_path_sum(root):
...