# Binary Tree Maximum Path Sum – Leetcode Solution

In this post, we are going to solve the 124. Binary Tree Maximum Path Sum problem of Leetcode. This problem 124. Binary Tree Maximum Path Sum is a Leetcode hard level problem. Let’s see the code, 124. Binary Tree Maximum Path Sum – Leetcode Solution.

## Problem

A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.

The path sum of a path is the sum of the node’s values in the path.

Given the `root` of a binary tree, return the maximum path sum of any non-empty path.

### Example 1 :

``````Input: root = [1,2,3]
Output: 6
Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.``````

### Example 2 :

``````Input: root = [-10,9,20,null,null,15,7]
Output: 42
Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.``````

### Constraints

• The number of nodes in the tree is in the range `[1, 3 * 104]`.
• `-1000 <= Node.val <= 1000`

Now, let’s see the code of 124. Binary Tree Maximum Path Sum – Leetcode Solution.

# Binary Tree Maximum Path Sum – Leetcode Solution

### 124. Binary Tree Maximum Path Sum – Solution in Java

```/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode() {}
*     TreeNode(int val) { this.val = val; }
*     TreeNode(int val, TreeNode left, TreeNode right) {
*         this.val = val;
*         this.left = left;
*         this.right = right;
*     }
* }
*/
class Solution {

int maxSum=Integer.MIN_VALUE;

public int sum(TreeNode root){
if(root == null) return 0;

int leftSum = sum(root.left);
int rightSum = sum(root.right);

int maxInLeftRight = Math.max(leftSum,rightSum);
int maxInAll = Math.max(root.val,maxInLeftRight+root.val);

int max = Math.max(maxInAll,root.val+leftSum+rightSum);

maxSum = Math.max(maxSum,max);
return maxInAll;
}

public int maxPathSum(TreeNode root) {
sum(root);
return maxSum;
}
}```

### 124. Binary Tree Maximum Path Sum – Solution in C++

```/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int solve(TreeNode* root,int &res)
{
// Base Case
if(root==NULL) return NULL;
int ls = solve(root->left,res);
int rs = solve(root->right,res);
int temp = max(max(ls,rs)+root->val,root->val);
int ans = max(temp,ls+rs+root->val);
res = max(res,ans);
return temp;
}
int maxPathSum(TreeNode* root) {
int res = INT_MIN;
solve(root,res);
return res;
}
};```

### 124. Binary Tree Maximum Path Sum– Solution in Python

```# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

class Solution:
def maxPathSum(self, root: TreeNode) -> int:
self.max_sum = float('-inf')
self.dfs(root)
return self.max_sum

def dfs(self, node):
if not node: return 0

leftST_sum = max(0, self.dfs(node.left))
rightST_sum = max(0, self.dfs(node.right))

self.max_sum = max(self.max_sum, leftST_sum + rightST_sum + node.val)

return max(leftST_sum, rightST_sum) + node.val
```

Note: This problem 124. Binary Tree Maximum Path Sum is generated by Leetcode but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.