-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path133.clone-graph.java
93 lines (80 loc) · 2.49 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
/*
* @lc app=leetcode id=133 lang=java
*
* [133] Clone Graph
*/
// @lc code=start
/*
// 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 {
// private HashMap<Node, Node> visited = new HashMap<>();
// public Node cloneGraph(Node node) {
// if (node == null) {
// return node;
// }
// // use recursion to build the graph deep copy
// // so if a node is visited, then we return the copy from hashmap
// if (visited.containsKey(node)) {
// return visited.get(node);
// }
// // copy the node,
// // to implement DEEP copy, we should not copy the neighbors,
// // but creat a new one
// Node cloneNode = new Node(node.val, new ArrayList());
// visited.put(node, cloneNode);
// for (Node neighbor : node.neighbors) {
// // recursion
// cloneNode.neighbors.add(cloneGraph(neighbor));
// }
// return cloneNode;
// }
// }
class Solution {
public Node cloneGraph(Node node) {
if (node == null) {
return node;
}
HashMap<Node, Node> visited = new HashMap<>();
LinkedList<Node> queue = new LinkedList<Node>();
queue.add(node);
Node cloneNode = new Node(node.val, new ArrayList<>());
visited.put(node, cloneNode);
while (!queue.isEmpty()) {
Node u = queue.poll();
for (Node neighbor : u.neighbors) {
// if not visited
if (!visited.containsKey(neighbor)) {
queue.add(neighbor);
Node cloneNeighbor = new Node(neighbor.val, new ArrayList<>());
visited.put(neighbor, cloneNeighbor);
}
// visited.get(u) -> get node u's copy node
// updatae the neighbors list of copy node,
// build the connection between copy node and its copy neighbors
visited.get(u).neighbors.add(visited.get(neighbor));
}
}
return cloneNode;
}
}
// @lc code=end