/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxPathSum(TreeNode root) {
if(root == null){
return 0;
}
int[] result = new int[2];
result = maxPathSumTracker(root);
return result[1];
}
private int[] maxPathSumTracker(TreeNode node){
int[] result = new int[2];
if(node == null){
result[1] = Integer.MIN_VALUE;
return result;
}
int[] resultLeft = maxPathSumTracker(node.left);
int[] resultRight = maxPathSumTracker(node.right);
result[0] = Math.max(Math.max(resultLeft[0], resultRight[0]) + node.val, node.value);
result[1] = Math.max(Math.max(resultLeft[1], resultRight[1]), Math.max(resultLeft[0] + node.val + resultRight[0], result[0]));
return result;
}
}