Maximum Path Sum between 2 Leaf Nodes | Practice | GeeksforGeeks

Given a binary tree in which each node element contains a number. Find the maximum possible path sum from one leaf node to another leaf node.
Note: Here Leaf node is a node which is connected to exactly one different node.

Example 1:
Input:      
           3                               
         /    \                          
       4       5                     
      /  \      
    -10   4                          
Output: 16
Explanation:
Maximum Sum lies between leaf node 4 and 5.
4 + 4 + 3 + 5 = 16.

Example 2:
Input:    
            -15                               
         /      \                          
        5         6                      
      /  \       / \
    -8    1     3   9
   /  \              \
  2   -3              0
                     / \
                    4  -1
                       /
                     10  
Output:  27
Explanation:
The maximum possible sum from one leaf node 
to another is (3 + 6 + 9 + 0 + -1 + 10 = 27)

Your Task:  
You dont need to read input or print anything. Complete the function maxPathSum() which takes root node as input parameter and returns the maximum sum between 2 leaf nodes.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of Tree)

Constraints:
2  ≤  Number of nodes  ≤  104
-103  ≤ Value of each node ≤ 103
0
akashkhurana2820 hours ago
JAVA EASY SOLUTION
int sum=Integer.MIN_VALUE;       int findMaxPathSum(Node root){           if(root==null)return 0;           int lsum=findMaxPathSum(root.left);           int rsum=findMaxPathSum(root.right);           if(root.left== null && root.right==null)return root.data;           if(root.left==null) return root.data+ rsum;           if(root.right==null) return root.data+lsum;           sum=Math.max(sum, root.data+lsum+rsum);           return Math.max(root.data+lsum, root.data+rsum);                  }              int maxPathSum(Node root)       {          int ans=findMaxPathSum(root);          if(root.left==null || root.right==null)               sum=Math.max(sum,ans);          return sum;                  }       }
0
saicharanthammi1 week ago
    int help(Node * root,int &m)   {       if(!root)        return 0;        if(!root->left && !root->right)  return root->data;       int l=help(root->left,m);       int r=help(root->right,m);       if(root->left && root->right)          {  m=max(m, root->data + l +r);            return root->data +max(l,r);          }       if(!root->left)            return root->data +r;         return root->data +l;    }   int maxPathSum(Node* root)   {   int maxi=INT_MIN;help(root,maxi);       if(!root ||root->left==NULL || root->right==NULL)             {   int k=0;                  if(!root)                    return 0;                   if(!root->left)                      return max(maxi,root->data+help(root->right,k));                    return max(maxi,root->data+help(root->left,k));               }                     return maxi;   }
-1
goelyash4 weeks ago
Easy c++ solution
class Solution {public:   int maxsum =INT_MIN;
   int maxPathSumUtil(Node* root)   {       if(!root)return 0;       int l = maxPathSumUtil(root->left);       int r= maxPathSumUtil(root->right);       if(root->left && root->right){       maxsum =max(maxsum,l+r+root->data);       return root->data+max(l,r);}       else if(root->left)return root->data+l;       else return root->data+r;   }   int maxPathSum(Node* root){       int x= maxPathSumUtil(root);       if(root->left && root->right)return maxsum;       return max(maxsum,x);   }};
0
aggarwaldeepali4541 month ago
int solve(Node *root, Node *par, int &ans){       if(root == NULL)           return 0;                 if(root->left == NULL && root->right == NULL)      return root->data;             if(root->left == NULL){          int temp =  solve(root->right, root, ans)  + root->data;                    if(par == root)          ans = max(temp, ans);          return temp;      }            if(root->right == NULL){          int temp = solve(root->left, root, ans) + root->data;                    if(par == root)          ans = max(ans, temp);                    return temp;      }              int l = solve(root->left,root,  ans);       int r = solve(root->right,root, ans);       int temp = max(l, r)  + root->data;                 ans = max(ans, root->data + l  + r);              return temp;          }   
   int maxPathSum(Node* root)   {       int ans = INT_MIN;       solve(root,root, ans);       return ans;   }
+1
harrypotter01 month ago
def maxPathSumUtil(root, res):

   if root is None:
       return 0
   ls = maxPathSumUtil(root.left, res)
   rs = maxPathSumUtil(root.right, res)
   if root.left is not None and root.right is not None:

       res[0] = max(res[0], ls + rs + root.data)

       return max(ls, rs) + root.data

   if root.left is None:
       return rs + root.data
   else:
       return ls + root.data
   return root.data+max(ls,rs)
class Solution:        
   def maxPathSum(self, root):
       res = [-float('inf')]
       ans = maxPathSumUtil(root, res)
       if not root.left or not root.right:
           res[0] = max(res[0], ans)
       return res[0]

0
amishasahu3281 month ago
class Solution {
public:
    int maxSum(Node *node, int &sum)
    {
        if(node == NULL) return 0;
        int leftSum = maxSum(node->left, sum);
        int rightSum = maxSum(node->right, sum);
        if(!node->left && !node->right)
            return node->data;
        if(!node->left)
            return node->data + rightSum;
        if(!node->right)
            return node->data + leftSum;
        sum = max(sum, leftSum + rightSum + node->data);
        return (node->data + max(leftSum, rightSum));
    }
    int maxPathSum(Node* root)
    {
        // code here
        int sum = INT_MIN;
        int ans = maxSum(root, sum);
        // Here Leaf node is a node which is connected to exactly one different node.
        // If root has only one child the it is also a leaf node
        // hence, will be counted
        if(!root->left || !root->right)
            sum = max(sum, ans);
        return sum;
    }
};
+8
bishtayush20011 month ago
why the solution for this test case is 16 ? 
5 N 6 -5 5
 
I think it would be 6 instead.
Infact the code given in editorial section failed at this point ... 
-2
piyushsagar7251 month ago
class Solution{   int mx = -9999999;   int maxPathSum(Node root)   {        maxPathSumUtil(root);       return mx;   }       int maxPathSumUtil(Node root) {       if (root == null)           return 0;                  int l = maxPathSumUtil(root.left);       int r = maxPathSumUtil(root.right);              mx = Math.max(mx, l + root.data + r);              if (l > r)           return l + root.data;       return r + root.data;   }}
0
bishtkunal092 months ago
int solve(Node* root,int& res){       if(root==NULL){           return 0;       }       if(root->left==NULL&&root->right==NULL){           return root->data;       }       if(root->left==NULL){           return solve(root->right,res)+root->data;       }       if(root->right==NULL){           return solve(root->left,res)+root->data;       }       int l=solve(root->left,res);       int r=solve(root->right,res);       int temp=max(l,r)+root->data;              int ans=l+r+root->data;       res=max(res,ans);       return temp;   }   int maxPathSum(Node* root)   {   int res=-1e9;       int val=solve(root,res); ;       if(res==-1e9){          return val;       }               return res;          }
0
binnukaniki1232 months ago
class Solution{   int max=Integer.MIN_VALUE;   int maxPathSum(Node root)   {             int p=m(root);        if(max ==Integer.MIN_VALUE )   {     return p;   }          return max;   }   int m(Node root){       if(root==null)         return 0;             int l=m(root.left);       int r=m(root.right);       if(root.left==null && root.right==null) return root.data;       if(root.left==null )return r+root.data;       if(root.right==null )return l+root.data;        int p= l+r+root.data;         if(max<p) max=p;                          if(l>r) return l+root.data;       else return r+root.data;   }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.