Max sum path code help

quesrtion–>
https://practice.geeksforgeeks.org/problems/maximum-path-sum/1#
code–>


it is not passing all testcases

@asaurabh_26 are u there?

yes i am seeing your code now

i have done serval things but using that approach some testcase are failing

use this code

ll ans;

ll maxSum(Node *root)
{
    if(root == NULL)
    return INT_MIN;
    
    ll l1 = maxSum(root->left);
    
    ll l2 = maxSum(root->right);
    
    if(!(root->left == NULL || root->right == NULL))
    ans = max(ans,l1 + l2 + (root->data));
    
    if(l1 == INT_MIN && l2 == INT_MIN)
    return (root->data);
    else
    return (max(l1,l2) + (root->data));
}

int maxPathSum(Node* root)
{
    ans = INT_MIN;
    // code here  
    ll v = maxSum(root);
    
    return ans;
    
}

bro i cannot understand what u did, can u explain in briefly why was not the code above was not working , and what did u do in this code?

try to understand this code

first call for left and right half
and then make 3 case

if(root->left && root->right)
   // both left and right exists
        ans = max(ans,left + right + (root->data));

    
if(root->left==NULL && root->right==NULL)
     // leaf node case
        return (root->data);

else
// in rest of the cases
        return (max(left,right) + (root->data));

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.