leetcode六月每日一题leetcode124

leetcode六月每日一题leetcode124_第1张图片
一天一道题也已经100道了,坚持哦
leetcode六月每日一题leetcode124_第2张图片

class Solution {

private:
    int maxPathSum(TreeNode* root, int& ans){
        if(!root) return 0;
        int l = max(0,maxPathSum(root->left,ans));
        int r = max(0,maxPathSum(root->right,ans));
        int sum = l + r + root->val;
        ans = max(ans,sum);
        return max(l,r) + root->val;
    }

public:
    int maxPathSum(TreeNode* root) {

        if(!root) return 0;
        int ans = INT_MIN;
        maxPathSum(root, ans);
        return ans;
    }
};

你可能感兴趣的:(leetcode)