forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_133.java
56 lines (48 loc) · 1.62 KB
/
_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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
public class _133 {
public static class Solution1 {
public Node cloneGraph(Node node) {
if (node == null) {
return node;
}
Map<Integer, Node> map = new HashMap();
Queue<Node> queue = new LinkedList();
Node root = new Node(node.val);
map.put(root.val, root);
//remember to offer the original input node into the queue which contains all the information
queue.offer(node);
while (!queue.isEmpty()) {
Node curr = queue.poll();
for (Node eachNode : curr.neighbors) {
if (!map.containsKey(eachNode.val)) {
map.put(eachNode.val, new Node(eachNode.val));
queue.offer(eachNode);
}
map.get(curr.val).neighbors.add(map.get(eachNode.val));
}
}
return root;
}
public static class Node {
public int val;
public List<Node> neighbors;
public Node() {
this.neighbors = new ArrayList<>();
}
public Node(int val) {
this.val = val;
this.neighbors = new ArrayList<>();
}
public Node(int val, List<Node> neighbors) {
this.val = val;
this.neighbors = neighbors;
}
}
}
}