Simple Ruby Level Order


  • 1
    R
    def largest_values(root)
        return [] if root.nil? 
        
        curr = [root]
        result = []
        
        while(!curr.empty?) do 
            result << curr.max_by(&:val).val
            curr = curr.each_with_object([]) { |node, array| array << node.left << node.right }.compact
        end
        result
    end
    

Log in to reply
 

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