forked from mdabarik/blind-75-leetcode-questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
27--clone-graph.java
51 lines (49 loc) · 1.21 KB
/
27--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
41
42
43
44
45
46
47
48
49
50
51
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}
*/
class Solution {
public Node cloneGraph(Node node) {
if (node == null) return null;
HashMap<Node, Node> map = new HashMap<>();
Node newNode = new Node(node.val);
map.put(node, newNode);
Queue<Node> queue = new LinkedList<>();
queue.add(node);
while (!queue.isEmpty()) {
Node curr = queue.poll();
List<Node> newNeighbors = map.get(curr).neighbors;
for (Node n : curr.neighbors) {
if (!map.containsKey(n)) {
Node tmp = new Node(n.val);
map.put(n, tmp);
queue.add(n);
}
newNeighbors.add(map.get(n));
}
}
return newNode;
}
}/*
1 -> 2, 4
2 -> 1, 3
3 -> 2, 4
4 -> 1, 3
1 : 1* -> 2*, 4*
2 : 2* ->
4 : 4* -> */