[LeetCode]Binary Tree Maximum Path Sum

public class Solution {
    private int result = Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
        helper(root);
        return result;
    }
    public int helper(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = helper(root.left);
        int right = helper(root.right);
        int[] array1 = new int[]{root.val, left + root.val, right + root.val, left + right + root.val};
        Arrays.sort(array1);
        result = Math.max(result, array1[3]);
        int[] array2 = new int[]{root.val, left + root.val, right + root.val};
        Arrays.sort(array2);
        return array2[2];
    }
}

 

posted @ 2015-12-04 07:34  Weizheng_Love_Coding  阅读(103)  评论(0)    收藏  举报