You are given the root of a binary tree. Your task is to find the maximum path sum.
A path is a sequence of connected nodes where:
Return the maximum possible sum of values along any valid path.
Input: root = [1,2,3]
Output: 6
Explanation: The maximum path is 2 → 1 → 3 with a sum of 6.
Input: root = [-10,9,20,null,null,15,7]
Output: 42
Explanation: The maximum path is 15 → 20 → 7 with a sum of 42.
Input: root = [-3]
Output: -3
Explanation: The only valid path consists of the single node.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
1
1
1 2 3
6
5 2 null 1 null 3
11