其他分享
首页 > 其他分享> > 腾讯精选324

腾讯精选324

作者:互联网

class Solution 
{
public:
   int maxPathSum(TreeNode* root, int &val)
{
 if (root == nullptr) return 0;
 int left = maxPathSum(root->left, val);
 int right = maxPathSum(root->right, val);
 int lmr = root->val + max(0, left) + max(0, right);
 int ret = root->val + max(0, max(left, right));
 val = max(val, max(lmr, ret));
 return ret;
}
int maxPathSum(TreeNode* root) 
{
 int val = INT_MIN;
 maxPathSum(root, val);
 return val;
}
};

标签:maxPathSum,right,val,int,max,精选,324,腾讯,root
来源: https://blog.csdn.net/weixin_45568353/article/details/100925585