Binary Tree Maximum Path Sum
Given the root of a binary tree, a path is any sequence of nodes connected by parent-child edges, where each node appears at most once and the path need not pass through the root. Return the maximum sum of node values along any such path.
Open official problem prompt ↗Find the largest possible sum of values on any connected path in the tree, where the path may start and end at any nodes and does not have to touch the root.
Imagine each node is a junction on a hiking trail with an elevation reward. At a junction you may combine the best downhill trail on the left with the best on the right to enjoy the whole view (recorded as the answer), but when you report a route to the junction above you, you can only hand off one downhill branch — you cannot walk two ways at once.
- Input
- root = [-10, 9, 20, null, null, 15, 7]
- Output
- 42
- Why
- The best path is 15 -> 20 -> 7 with sum 15 + 20 + 7 = 42.
The number of nodes is in the range [1, 3 * 10^4]-1000 <= Node.val <= 1000