InferenceAI

Enter at least two characters to search.

Start free

Maximum Path Sum

hardTrees40 min50 XP

A path is a sequence of connected nodes, each used at most once; it can start and end anywhere. Return the largest sum of node values over all non-empty paths.

Trees are given in level order as a list, with None for a missing child (for example [3, 9, 20, None, None, 15, 7]).

Examples

EXAMPLE WALKTHROUGHsimple

A guided view of the published example, not a trace of your code.

INPUT[[1,2,3]]
TASKmax_path_sum()
EXPECTED OUTPUT6

Start with the input values shown in this example.

Example 1 of 3

Input: values = [1,2,3]

Output: 6

Explanation: simple

Input: values = [-10,9,20,null,null,15,7]

Output: 42

Explanation: skip the root

Input: values = [-3]

Output: -3

Explanation: single negative

Hint 1

For each node, compute the best downward path starting there (ignore negative branches).

Hint 2

The best path through a node is node + best left gain + best right gain.

Topics

DSATreesPython