LeetCode 133. Clone Graph
考点 | 难度 |
---|---|
Graph | Medium |
题目
Given a reference of a node in a connected undirected graph.
Return a deep copy (clone) of the graph.
Each node in the graph contains a value (int
) and a list (List[Node]
) of its neighbors.
class Node {
public int val;
public List<Node> neighbors;
}
Test case format:
For simplicity, each node’s value is the same as the node’s index (1-indexed). For example, the first node with val == 1
, the second node with val == 2
, and so on. The graph is represented in the test case using an adjacency list.
An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.
The given node will always be the first node with val = 1
. You must return the copy of the given node as a reference to the cloned graph.
思路
两步:1. 用BFS遍历graph 2. 用hash map记录已经visit & clone过的node
对于每一个没有clone过的node,需要先clone,然后move on到它的neighbor (BFS)。如果neighbor已经被clone过,只需要把它(neighbor)加到现在这个node被clone过版本的neighbors list。如果neighbor没有被clone过,需要先clone它 & 放进queue,再加到list。
答案
class Solution:
def cloneGraph(self, node: 'Node') -> 'Node':
if not node: return node
q, clones = deque([node]), {node.val: Node(node.val, [])}
while q:
cur = q.popleft()
cur_clone = clones[cur.val]
for ngbr in cur.neighbors:
if ngbr.val not in clones:
clones[ngbr.val] = Node(ngbr.val, [])
q.append(ngbr)
cur_clone.neighbors.append(clones[ngbr.val])
return clones[node.val]