首页 > 其他 > 详细

[LeetCode]Binary Tree Maximum Path Sum

时间:2015-12-04 07:53:25      阅读:374      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!