forked from singlemancombat/interview-preparation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloneGraph.java
More file actions
56 lines (48 loc) · 1.95 KB
/
Copy pathCloneGraph.java
File metadata and controls
56 lines (48 loc) · 1.95 KB
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
/**
* 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 node;
Map<Integer, UndirectedGraphNode> map = new HashMap<>();
Deque<UndirectedGraphNode> queue = new ArrayDeque<>();
UndirectedGraphNode copyNode = new UndirectedGraphNode(node.label);
map.put(node.label, copyNode);
queue.offer(node);
while (!queue.isEmpty()) {
UndirectedGraphNode currNode = queue.poll();
for (UndirectedGraphNode neighbor: currNode.neighbors) {
if (! map.containsKey(neighbor.label)) {
UndirectedGraphNode copyNeighbor = new UndirectedGraphNode(neighbor.label);
map.put(neighbor.label, copyNeighbor);
queue.offer(neighbor);
}
map.get(currNode.label).neighbors.add(map.get(neighbor.label));
}
}
return copyNode;
}
}
public class Solution {
Map<Integer, UndirectedGraphNode> map = new HashMap<>();
public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
return clone(node);
}
private UndirectedGraphNode clone(UndirectedGraphNode node) {
if (node == null) return null; // return null
UndirectedGraphNode value = map.get(node.label);
if (value != null) return value;
UndirectedGraphNode cur = new UndirectedGraphNode (node.label);
map.put(cur.label, cur);
for (UndirectedGraphNode neighbor : node.neighbors) { // 不用while 用recuresive把所有点都遍历完
cur.neighbors.add(clone(neighbor));
}
return cur;
}
}