Code failing on the 52nd test out of 54--no idea why. Ruby


  • 0
    S

    Hi all--question is in the title. Can't figure out what's wrong and the input for the 52nd test is huge so its tough to manually debug. It's not a TLE error--the expected output and my output are off by a few numbers. I would post the test case input here but it's really far too large and would take up this entire post. Any ideas of what's wrong conceptually?

    
    
    # @param {TreeNode} root
    # @return {Integer}
    def longest_consecutive(root)
        length(root)
    end
    def length(node)
        return 0 if node.nil?
        current_val = node.val
        max_len = 1
        if node.left 
            if node.left.val == current_val + 1
                max_len = [max_len, 1 + length(node.left)].max
            else 
                max_len = [max_len, length(node.left)].max #start a new one 
            end
        end
        
        if node.right 
            if node.right.val == current_val + 1
                max_len = [max_len, 1 + length(node.right)].max
            else
                max_len = [max_len, length(node.right)].max
            end
        end
        
        max_len
    end
    

Log in to reply
 

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