124.Binary Tree Maximum Path Sum
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/39775/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int maxValue;
public int maxPathSum(TreeNode root) {
maxValue = Integer.MIN_VALUE;
helper(root);
return maxValue;
}
public int helper(TreeNode root){
if(root == null) return 0;
int left = Math.max(0, helper(root.left));
int right = Math.max(0, helper(root.right));
maxValue = Math.max(left + right + root.val, maxValue);
return Math.max(left, right) + root.val;
}
}