895. Binary Tree Maximum Path Sum

MediumTreesBinary TreeTree DPDFS

Given the root of a binary tree, return the maximum path sum of any non-empty path. A path is a sequence of nodes connected by edges (each node appearing once) and need not pass through the root. The tree is given as a level-order array with null for missing children.

Input: A level-order array of the tree.

Output: Integer — the maximum path sum.

Examples

Example 1
Input: [-10,9,20,null,null,15,7]
Output: 42
Explanation: Path 15-20-7 sums to 42.
Example 2
Input: [1,2,3]
Output: 6
Explanation: Path 2-1-3 sums to 6.
Example 3
Input: [-3]
Output: -3
Explanation: Single negative node.

Constraints

Asked by

AdobeAmazonMetaMicrosoftGoogleBloomberg
Solve this problem in the editor →