Ruby solution inspired by dietpepsi


  • 0
    B
    def is_valid_serialization(preorder)
        nodes = preorder.split(',')
        diff = 1
        nodes.each do |node|
            diff -= 1
            return false if diff < 0
            diff += 2 if node != '#'
        end
        return diff == 0
    end
    

Log in to reply
 

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