解题思路

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);}}
