Can anyone tell what is wrong with this code?


  • 0
    D

    I've submitted a code and get accepted. But before that, I submitted the following code and get it wrong, can anyone figure it out where it goes wrong? (quite interesting ^^) If you can figure it out by just reading the code, it's great ^^

    public class Solution {
        int maxNumber = Integer.MIN_VALUE;
        public int maxPathSum(TreeNode root){
            maxNumber = root.val;
            maxNumber = Math.max(maxNumber, m(root));
            return maxNumber;
        }
        public int m(TreeNode root){
            int l=Integer.MIN_VALUE;
            int r=Integer.MIN_VALUE;
            if(root.left != null) 
                l = m(root.left);
            if(root.right!= null)
                r = m(root.right);
            int mm = Math.max(l, r);
            if(l==Integer.MIN_VALUE) l = 0;
            if(r==Integer.MIN_VALUE) r = 0;
    
            maxNumber = Math.max(Math.max(maxNumber, mm),l+root.val+r);
           
            return Math.max(root.val, Math.max(l+root.val, r+root.val));
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.