forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminji-go.java
More file actions
28 lines (22 loc) · 779 Bytes
/
minji-go.java
File metadata and controls
28 lines (22 loc) · 779 Bytes
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
/**
* <a href="https://leetcode.com/problems/clone-graph/">week8-3.clone-graph</a>
* <li>Description: Return a deep copy (clone) of the graph</li>
* <li>Topics: Hash Table, Depth-First Search, Breadth-First Search, Graph</li>
* <li>Time Complexity: O(N+E), Runtime 26ms</li>
* <li>Space Complexity: O(N), Memory 42.77MB</li>
*/
class Solution {
private Map<Node, Node> map = new HashMap<>();
public Node cloneGraph(Node node) {
if(node == null) return null;
if (map.containsKey(node)) {
return map.get(node);
}
Node clone = new Node(node.val);
map.put(node, clone);
for(Node neighbor : node.neighbors) {
clone.neighbors.add(cloneGraph(neighbor));
}
return clone;
}
}