124. Binary Tree Maximum Path Sum

class Solution {
    int maxPathSum;
    public int maxPathSum(TreeNode root) {
        maxPathSum=Integer.MIN_VALUE;
        seachPathSum(root);
        return maxPathSum;
    }
    private int seachPathSum(TreeNode node){
        if(node==null)
            return 0;
        int left=Math.max(0, seachPathSum(node.left));
        int right=Math.max(0, seachPathSum(node.right));
        maxPathSum=Math.max(left+right+node.val, maxPathSum);
        return Math.max(left, right)+node.val;
    }
}

  

posted @ 2017-10-03 02:12  Weiyu Wang  阅读(119)  评论(0编辑  收藏  举报