Ruby Solution using BFS Traversal


  • 0
    M
    def min_depth(root)
        return 0 if root.nil?
        root_node = [ root, 1 ]
        queue = [root_node]
        
        until queue.empty?
            parent, depth = queue.shift
            if parent.left.nil? && parent.right.nil?
                return depth
            else
                queue << [parent.left,  depth + 1] if parent.left
                queue << [parent.right, depth + 1] if parent.right
            end
        end
    end
    

Log in to reply
 

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