class Solution {
public:
int res = -INT_MAX;
int dfs(TreeNode* root){
if(!root) return 0;
int left = dfs(root->left);//左子树的最大路径和
int right = dfs(root->right);//右子树的最大路径和
int innerMax = root->val + left + right;//当前子树内部最大和
res = max(innerMax, res);
int tempMax = max(left, right);
int outterMax = root->val + max(0, tempMax);//子树给上级提供的最大和
if(outterMax < 0) return 0;
return outterMax;
}
int maxPathSum(TreeNode* root) {
if(!root) return 0;
dfs(root);
return res;
}
};