leetcode 133. 克隆图 发表于 2018-12-25 | 分类于 leetcode 题目链接:leetcode 思路:bfs 12345678910111213141516171819from collections import dequeclass Solution: # @param node, a undirected graph node # @return a undirected graph node def cloneGraph(self, node): if node == None: return root = UndirectedGraphNode(node.label) q = deque([(node, root)]) # node to complete neighbors visited = set() while q: n, tobe = q.popleft() visited.add(n) for neighbor in n.neighbors: neighbor_tobe = UndirectedGraphNode(neighbor.label) tobe.neighbors.append(neighbor_tobe) if neighbor not in visited: q.append((neighbor, neighbor_tobe)) return root