-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloneGraph_133.java
58 lines (51 loc) · 1.88 KB
/
CloneGraph_133.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Definition for undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/
/**
* Depth-First
*/
/*public class Solution {
Map<Integer, UndirectedGraphNode> map = new HashMap<>(); // map of new GraphNode
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null) return null;
if (map.containsKey(node.label)) return map.get(node.label);
UndirectedGraphNode newnode = new UndirectedGraphNode(node.label);
map.put(newnode.label, newnode);
for (UndirectedGraphNode n : node.neighbors) {
// XXXXXX SLEF LOOP!!!!!!!!!
if (n.label == newnode.label) newnode.neighbors.add(map.get(newnode.label));
else newnode.neighbors.add(cloneGraph(n));
}
return newnode;
}
}*/
/**
* Breadth-First
*/
public class Solution {
Map<Integer, UndirectedGraphNode> map = new HashMap<>();
Queue<UndirectedGraphNode> q = new LinkedList<>(); // the original nodes that have not copied neighbors
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
if (node == null) return null;
UndirectedGraphNode head = new UndirectedGraphNode(node.label);
UndirectedGraphNode ocurr = node, curr = head;
map.put(curr.label, curr);
while(ocurr != null) {
curr = map.get(ocurr.label);
for(UndirectedGraphNode n : ocurr.neighbors) {
if (!map.containsKey(n.label)) {
map.put(n.label, new UndirectedGraphNode(n.label));
q.offer(n);
}
curr.neighbors.add(map.get(n.label));
}
ocurr = q.poll();
}
return head;
}
}