Skip to content
Decoder

Maximum Path Sum

Hard

Problem

A path in a binary tree is a chain of nodes where each pair of neighbours is connected by an edge, and no node appears twice. It does not have to go through the root and it can bend at one node (go up, then down). A path has at least one node.

Given the root, return the largest sum of values along any path.

Trees in the examples are written in level order (top to bottom, left to right), with null for a missing child, the same way the tests build them.

Example 1

Input
[1, 2, 3]
Output
6
Why
2 -> 1 -> 3

Example 2

Input
[-10, 9, 20, null, null, 15, 7]
Output
42
Why
15 -> 20 -> 7

Example 3

Input
[-3]
Output
-3

Constraints

  • 1 <= number of nodes <= 3 * 10^4
  • -1000 <= Node.val <= 1000

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.