class Solution {
public:
int max_sum=INT32_MIN;
int maxpath(TreeNode* root)
{
if(root==NULL)
return 0;
int leftvalue=max(0,maxpath(root->left));
int rightvalue=max(0,maxpath(root->right));
int pathvalue=root->val+leftvalue+rightvalue;
if(pathvalue>max_sum)
max_sum=pathvalue;
return root->val+max(leftvalue,rightvalue);
}
int maxPathSum(TreeNode* root)
{
maxpath(root);
return max_sum;
}
};
leetcode 124
最新推荐文章于 2023-10-30 19:27:54 发布