Skip to content

Commit 649490e

Browse files
committed
feat: clone-graph
1 parent 700dca6 commit 649490e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

clone-graph/minji-go.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/clone-graph/">week8-3.clone-graph</a>
3+
* <li>Description: Return a deep copy (clone) of the graph</li>
4+
* <li>Topics: Hash Table, Depth-First Search, Breadth-First Search, Graph</li>
5+
* <li>Time Complexity: O(N+E), Runtime 26ms</li>
6+
* <li>Space Complexity: O(N), Memory 42.77MB</li>
7+
*/
8+
9+
class Solution {
10+
private Map<Node, Node> map = new HashMap<>();
11+
12+
public Node cloneGraph(Node node) {
13+
if(node == null) return null;
14+
15+
if (map.containsKey(node)) {
16+
return map.get(node);
17+
}
18+
19+
Node clone = new Node(node.val);
20+
map.put(node, clone);
21+
22+
for(Node neighbor : node.neighbors) {
23+
clone.neighbors.add(cloneGraph(neighbor));
24+
}
25+
26+
return clone;
27+
}
28+
}

0 commit comments

Comments
 (0)