classSolution{ int max=Integer.MIN_VALUE; public int maxPathSum(TreeNode root) { if(root==null){ return0; } helper(root); returnmax; } public int helper(TreeNode root){ if(root==null){ return0; } int left=helper(root.left); int right=helper(root.right); int temp=root.val+(Math.max(left,0)+Math.max(right,0)); max=Math.max(max,temp); return root.val+Math.max(Math.max(left,0),Math.max(right,0)); } }