Help me out in this problem


Hey @shivammishra20121999 . I am seeing your problem and code , till then be patient.

@mr.encoder okay bro

I guess you know the concept but you aren’t able to implement it properly because the maximum you are taking in line 9-12 won’t give desired output.
See this editorial


It has been explained well also :slight_smile:

class Solution {
public:
int res=INT_MIN;
int maxPathSum(TreeNode* root) {
if(root==NULL)
{
return 0;
}
int lsum=maxPathSum(root->left);
int rsum=maxPathSum(root->right);
int mx1=max(lsum+root->val,rsum+root->val);
int mx2=max(mx1,root->val);
int mx3=max(mx2,lsum+rsum+root->val);
if(mx3>res)
{
res=mx3;
}
return mx2;

}

};
@mr.encoder bro logic is same

I am saying this only , the thing I want you to focus is just check the max function. What values this code is calculating. You will figure out the mistake :slight_smile:

And one more thing
In the given code it’s doing

returnpath=currmax;
        
        currmax=max(currmax,sumleft+sumright+root->val);
        ans=max(ans,currmax);

        return returnpath;

So I would suggest you to also consider this :slight_smile:

@mr.encoder exactly the same thing my code is also doing ??

Wait let me submit your code , I’ll let you know .

@mr.encoder you got the mistake ??

The logic you have used after line 12, i am not getting it from there. . Your max statements are also creating confusions . Do one thing write the code which i have given to you with different name of data type like you did in lsum rsum and then try to find your mistake.