美文网首页
124 Binary Tree Maximum Path Sum

124 Binary Tree Maximum Path Sum

作者: 火焰婆婆 | 来源:发表于2015-09-11 06:39 被阅读0次

    124
    Binary Tree Maximum Path Sum
    21.6%
    Hard
    singlePathSum 必须经过自己

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

    相关文章

      网友评论

          本文标题:124 Binary Tree Maximum Path Sum

          本文链接:https://www.haomeiwen.com/subject/nzypcttx.html