Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Binary Tree Maximum Path Sum

Hard
treebinary-treedfsdynamic-programming

A path is any sequence of adjacent nodes (each node used at most once) — it need not pass through the root or reach a leaf. Return the maximum possible sum of values along any non-empty path.

Input: one line — the tree in level order. Output: the maximum path sum.

Example 1
Input:
1 2 3
Output:
6

2 → 1 → 3.

Example 2
Input:
-10 9 20 null null 15 7
Output:
42

15 → 20 → 7.

Constraints

  • 1 ≤ nodes ≤ 3·10^4
  • -1000 ≤ values ≤ 1000
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.