其他分享
首页 > 其他分享> > LeetCode 124

LeetCode 124

作者:互联网

https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/

这个题明明有思路,但是写出来的代码就是一团糟还过不了,一气之下直接放弃治疗看讨论区了。。

先贴代码

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

https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/comments/3015

标签:right,int,124,getMax,realMax,root,LeetCode,left
来源: https://www.cnblogs.com/ZJPaang/p/12721042.html