Simple ruby O(n)


  • 0
    T
    def longest_consecutive(root)
        return search(root, nil, 0)
    end
    
    def search(root, parent, count)
        return count if root.nil?
        count = ( parent && root.val == parent.val + 1 ) ? count + 1 : 1
        return [count, search(root.left, root, count), search(root.right, root, count)].max
    end
    

Log in to reply
 

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