树的路径的最大和是多少

https://practice.geeksforgeeks.org/problems/maximum-path-sum/1 this is question from geeks for geeks. i wrote my ans. but it is giving wrong. what is problem with my logic?

int path(Node *root, int & max_sum)
{
  if(root==NULL)
    return 0;
  int l=path(root->left,max_sum);
  int r=path(root->right,max_sum);
  if(root->left && root->right)
  {
    max_sum=max(max_sum,l+r+root->data);
    return max(l,r)+root->data;
  }
  return (!root->left)? r + root->data:l + root->data; 
}

int maxPathSum(Node *root) 
{
  int max_sum=INT_MIN;
  path(root,max_sum);
  return max_sum;
  // code here
}
评论