class Solution {
int result = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
dfs(root);
return result;
}
public int dfs(TreeNode root){
if(root == null) return 0;
// 左右子树的最大值,若是负数就舍弃
int left = Math.max(dfs(root.left), 0);
int right = Math.max(dfs(root.right), 0);
// 与之前的最大值作比较
result = Math.max(result, root.val + left +right);
return root.val + Math.max(left, right);
}
}