leetcode124. 二叉树中的最大路径和

class Solution {
public:
    int ans=INT_MIN;
    int dfs(TreeNode*root){
        if(!root)return 0;
        int l=max(dfs(root->left),0);
        int r=max(dfs(root->right),0);
        ans=max(ans,root->val+l+r);
        return root->val+max(l,r);
    }
    int maxPathSum(TreeNode* root) {
        dfs(root);
        return ans;
    }
};