Open In App

Maximum Path Sum in a Binary Tree

Last Updated : 03 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given a binary tree, the task is to find the maximum path sum. The path may start and end at any node in the tree.

Example: 

Input: 

tree-2


Output: 42
Explanation: Max path sum is represented using green colour nodes in the above binary tree.

Input:

Tree-Image

Output: 31
Explanation: Max path sum is represented using green colour nodes in the above binary tree.

Approach:

If the maximum sum path in a binary tree passes through the root, there are four possible cases to consider:

  1. The path consists only the root itself, without involving any children.
  2. The path starts at the root and extends downward through its right child, possibly continuing to the bottom of the right subtree.
  3. The path starts at the root and extends downward through its left child, possibly continuing the bottom of the left subtree.
  4. The path includes the root and spans both the left and right children.

The idea is to keep track of all four paths for each subtree of the tree and pick up the max one in the end.

Implementation:

For each subtree, we want to find the maximum path sum that starts at its root and goes down through either its left or right child. To do this, we create a recursive function (lets say MPS(current root)) that calculates and returns the maximum path sum starting from the root and extending downward through one of its children.

Within the same function, we can also calculate the maximum path sum for the current subtree and compare it with the final answer. This is done by combining the MPS(current root -> left) and MPS(current root -> right) with the value of the current root.

Note that if the MPS() from the left or right child is negative, we simply ignore it while calculating the maximum path sum for the current subtree.

C++


// Returns the maximum path sum in the subtree with the current node as an endpoint. 
// Also updates 'res' with the maximum path sum.
int maxPathSumUtil(Node* root, int& res) {
  
    // Base case: return 0 for a null node
    if (root == NULL)
        return 0;

    // Calculate maximum path sums for left and right subtrees
    int l = max(0, maxPathSumUtil(root->left, res));
    int r = max(0, maxPathSumUtil(root->right, res));

    // Update 'res' with the maximum path sum passing through the current node
    res = max(res, l + r + root->data);

    // Return the maximum path sum rooted at this node
    return root->data + max(l, r);
}

// Returns maximum path sum in tree with given root
int maxPathSum(Node* root) {
    int res = root->data;
	
  	// Compute maximum path sum and store it in 'res'
    maxPathSumUtil(root, res);
  
    return res;
}


Java Python C# JavaScript

Output
42

Time Complexity: O(n), where n is the number of nodes in the Binary Tree.
Auxiliary Space: O(h), where h is the height of the tree.



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg