/**
* Definition for Undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) {
* label = x;
* neighbors = new ArrayList<UndirectedGraphNode>();
* }
* }
*/
public class Solution {
/**
* @param node: A undirected graph node
* @return: A undirected graph node
*/
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null) return node;
Map<UndirectedGraphNode, UndirectedGraphNode> oldToNew = new HashMap<>();
Queue<UndirectedGraphNode> q = new ArrayDeque<>();
q.offer(node);
oldToNew.put(node, new UndirectedGraphNode(node.label));
while (!q.isEmpty()) {
UndirectedGraphNode curr = q.poll();
for (UndirectedGraphNode nei : curr.neighbors) {
if (!oldToNew.containsKey(nei)) {
oldToNew.put(nei, new UndirectedGraphNode(nei.label));
q.offer(nei);
}
oldToNew.get(curr).neighbors.add(oldToNew.get(nei));
}
}
return oldToNew.get(node);
}
}