Skip to content

Maximum Path Sum

HardTrees
Decode it first. Clues, constraints, pattern and plan before any code.

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.

[1, 2, 3]                        →  6    (2 -> 1 -> 3)
[-10, 9, 20, null, null, 15, 7]  →  42   (15 -> 20 -> 7)
[-3]                             →  -3

Constraints

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