Simple Python BFS


  • 0
    def findSecondMinimumValue(self, root):
        res = []
        queue = [root]
        seen = set()
        for node in queue:
            if node.val not in seen:
                heapq.heappush(res, node.val)
                seen.add(node.val)
            if node.left:
                queue.append(node.left)
                queue.append(node.right)
        heapq.heappop(res)
        return heapq.heappop(res) if res else -1

Log in to reply
 

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