Failed two test cases, can't find the reason, anyone can help?


  • 0
    B
     public int longestConsecutive(TreeNode root) {
            if(root == null)
                return 0;
            if(root.left == null && root.right == null)
                return 1;
            int left = 0, right = 0;
            if(root.left != null){
                if(root.val + 1 == root.left.val)
                    left = longestConsecutive(root.left) + 1;
                else
                    left = longestConsecutive(root.left);
            }
            if(root.right != null){
                if(root.val + 1 == root.right.val)
                right = longestConsecutive(root.right) + 1;
    
                else 
                    right = longestConsecutive(root.right);
            }
            return Math.max(right, left);
        }
    

    Failed two test cases, can't find the reason, anyone can help?


  • 2
    R

    Try this example:

    [1,2,null,2,null,3]

  • 0
    B

    It should return 2 right? I think my code works for this case


  • 0
    X

    @rwalk333 Thanks for this test case!


Log in to reply
 

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