解题思路
class Solution {
int res = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
helper(root);
return res;
}
private int helper(TreeNode root) {
if (root == null) return 0;
//左边最大值
int left = helper(root.left);
//右边最大值
int right = helper(root.right);
//和全局变量比较
res = Math.max(left + right + root.val, res);
// >0 说明都能使路径变大
return Math.max(0, Math.max(left, right) + root.val);
}
}