-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path133. Clone Graph.java
40 lines (36 loc) · 1.56 KB
/
133. Clone Graph.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
public class Solution {
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if(node == null) return null;
Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<>(); // <Raw, Cloned>
Queue<UndirectedGraphNode> queue = new LinkedList<>(); // <Raw>
map.put(node, new UndirectedGraphNode(node.label));
queue.offer(node);
// BFS
while(!queue.isEmpty()) {
// raw and cloned parent
UndirectedGraphNode rawParent = queue.poll();
UndirectedGraphNode cloneParent = map.get(rawParent);
// cloned parent's children
List<UndirectedGraphNode> cloneChildren = cloneParent.neighbors;
// raw parent's children
for(UndirectedGraphNode rawChild : rawParent.neighbors) {
if(!map.containsKey(rawChild)) {
// if map does not contain it, we need to 'new' it and put it into queue
map.put(rawChild, new UndirectedGraphNode(rawChild.label));
queue.offer(rawChild);
}
// no matter it is new or not, we need to put it into children
cloneChildren.add(map.get(rawChild));
}
}
return map.get(node);
}
}