码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Binary Tree Maximum Path Sum

时间:2015-12-04 07:55:16      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

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

 

[LeetCode]Binary Tree Maximum Path Sum

标签:

原文地址:http://www.cnblogs.com/vision-love-programming/p/5018234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!