No Helper function, Please tell me why it is not working.


  • 0

    I have used simple recursion without a helper function. Only 52 cases are passing out of 54. Don't understand why..

    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 && root.val+1 == root.left.val)
                left++;
            if(root.right != null && root.val+1 == root.right.val)
                right++;
            left += longestConsecutive(root.left);
            right += longestConsecutive(root.right);
            return Math.max(left,right);
        }

Log in to reply
 

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