← DiffPush

Maximum Path Sum in Binary Tree

DiffPush Tier Binary Trees · Medium Problems O(n) · O(h)

The Peak-Payout Delivery Route

A courier drone flies a binary-tree network of pickup towers, each carrying a parcel value that can be negative (a disposal fee). A route may start at any tower, follow connected links, and stop at any tower — but never revisits one. The dispatcher wants the maximum total value achievable, knowing sometimes the best route is a single tower and negative branches should be abandoned mid-flight.

Input: The root of a binary tree given as a level-order array where null marks a missing child. Values may be negative.

Output: Return an integer: the maximum sum of node values over any non-empty path in the tree.

Constraints

Examples

Example 1

Input: {"tree":[1,2,3]}
Output: 6
The bend 2 -> 1 -> 3 collects all three positive towers.

Example 2

Input: {"tree":[-10,9,20,null,null,15,7]}
Output: 42
Skip the negative root; the path 15 -> 20 -> 7 sums to 42.

Solve this in your browser →

Also on LeetCode ↗