**Problem Statement:** Write a program to find the maximum sum path in a binary tree. A path in a binary tree is a sequence of nodes where every adjacent pair of nodes are connected by an edge. A node can only appear in the sequence at most once. A path need not pass from the root. We need to find the path with the maximum sum in the binary tree.

**Example:**

Input:Output:The Max Path Sum for the Tree is 42

** Disclaimer**:

*Don’t jump directly to the solution, try it out yourself first.*

**Solution:**

**Approach: **A brute force approach would be to generate all paths and compare them. Generating all paths will be a time-costly activity therefore we need to look for something else.

We first need to define what is the maximum path sum through a given node (when that node is acting as the root node/curving point). At a given node with a value, if we find the max leftSumPath in the left-subtree and the max rightSumPath in the right subtree, then the maxPathSum through that node is value+(leftSumPath+rightSumPath).

Now we can apply this formula at every node by doing a simple tree traversal and storing the maximum value (our answer) in a reference variable.

For our recursion to work, it is very important to understand what value we return from our function. In our recursive function, we find and compare the maxPathSum from a given node when it is the root/turning point of the path. But what we return is the maxPathSum of that same node when it is **NOT **the root/turning point of the path. To find the latter maxPath, we no longer have the liberty to consider both leftMaxPath and rightMaxPath, we will simply take the maximum of the two and it to the value of the node.

To summarize:

- Initialize a maxi variable to store our final answer.
- Do a simple tree traversal. At each node, find recursively its leftMaxPath and its rightMaxPath.
- Calculate the maxPath through the node as val + (leftMaxPath + rightMaxPath) and update maxi accordingly.
- Return the maxPath when node is not the curving point as val + max(leftMaxPath, rightMaxPath).

**Code:**

## C++ Code

```
#include <bits/stdc++.h>
using namespace std;
struct node {
int data;
struct node * left, * right;
};
int findMaxPathSum(node * root, int & maxi) {
if (root == NULL) return 0;
int leftMaxPath = max(0, findMaxPathSum(root -> left, maxi));
int rightMaxPath = max(0, findMaxPathSum(root -> right, maxi));
int val = root -> data;
maxi = max(maxi, (leftMaxPath + rightMaxPath) + val);
return max(leftMaxPath, rightMaxPath) + val;
}
int maxPathSum(node * root) {
int maxi = INT_MIN;
findMaxPathSum(root, maxi);
return maxi;
}
struct node * newNode(int data) {
struct node * node = (struct node * ) malloc(sizeof(struct node));
node -> data = data;
node -> left = NULL;
node -> right = NULL;
return (node);
}
int main() {
struct node * root = newNode(-10);
root -> left = newNode(9);
root -> right = newNode(20);
root -> right -> left = newNode(15);
root -> right -> right = newNode(7);
int answer = maxPathSum(root);
cout << "The Max Path Sum for this tree is " << answer;
return 0;
}
```

The Max Path Sum for this tree is 42

**Time Complexity: O(N)**.

Reason: We are doing a tree traversal.

**Space Complexity: O(N)**

Reason: Space is needed for the recursion stack. In the worst case (skewed tree), space complexity can be O(N).

## Java Code

```
import java.util.*;
class Node {
int val;
Node left, right;
Node(int val) {
this.val = val;
left = null;
right = null;
}
}
class TUF {
public static int maxPathSum(Node root) {
int maxValue[] = new int[1];
maxValue[0] = Integer.MIN_VALUE;
maxPathDown(root, maxValue);
return maxValue[0];
}
public static int maxPathDown(Node node, int maxValue[]) {
if (node == null) return 0;
int left = Math.max(0, maxPathDown(node.left, maxValue));
int right = Math.max(0, maxPathDown(node.right, maxValue));
maxValue[0] = Math.max(maxValue[0], left + right + node.val);
return Math.max(left, right) + node.val;
}
public static void main(String args[]) {
Node root = new Node(-10);
root.left = new Node(9);
root.right = new Node(20);
root.right.left = new Node(15);
root.right.right = new Node(7);
int answer = maxPathSum(root);
System.out.println("The Max Path Sum for this tree is " + answer);
}
}
```

The Max Path Sum for this tree is 42

**Time Complexity: O(N)**.

Reason: We are doing a tree traversal.

**Space Complexity: O(N)**

Reason: Space is needed for the recursion stack. In the worst case (skewed tree), space complexity can be O(N).

Special thanks toplease check out this articleAnshuman Sharmafor contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam,