Max path sum bt

code: https://ide.codingblocks.com/s/578321

problem: https://leetcode.com/problems/binary-tree-maximum-path-sum/

The code has been accepted but I had a doubt,
in line 34 if I do not add 0 in return max(0,max(op3,op4));
it is giving the wrong answer but why? because even if the path sum is negative in that subtree we have to return it right ? but here we are returning 0 instead

int op1 = root->value + left + right; // max path sum passes through the root
int op2 = root->value ;// left and right path sums are negative
int op3 = left + root->value ;
int op4 = right + root->value;

at line 34 basically u should return max(op2, max(op3,op4)) because for the above tree max ans could be this root itself or this root->val + ans from right subtree or root->val + ans from left subtree.
i am unable to understand why u are doing max with 0

https://ide.codingblocks.com/s/578324 this is my code

thanks a lot makes sense now, earlier I was only returning left path sum and right path sum didn’t realise that the root can be returned too

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.