File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ - ๋ฌธ์ : https://leetcode.com/problems/clone-graph/
2+ - ํ์ด: https://algorithm.jonghoonpark.com/2024/02/13/leetcode-133
3+
4+ ``` java
5+ class Solution {
6+ public Node cloneGraph (Node node ) {
7+ return cloneGraph(new HashMap<> (), node);
8+ }
9+
10+ private Node cloneGraph (Map<Integer , Node > map , Node node ) {
11+ if (node == null ) {
12+ return null ;
13+ }
14+
15+ if (map. containsKey(node. val)) {
16+ return map. get(node. val);
17+ }
18+
19+ Node copy = new Node (node. val);
20+ map. put(node. val, copy);
21+
22+ for (int i = 0 ; i < node. neighbors. size(); i++ ) {
23+ Node neighborNode = node. neighbors. get(i);
24+ copy. neighbors. add(map. getOrDefault(neighborNode. val, cloneGraph(map, node. neighbors. get(i))));
25+ }
26+
27+ return copy;
28+ }
29+ }
30+ ```
31+
32+ ### TC, SC
33+
34+ node(vertex)์ ์๋ฅผ ` V ` , edge์ ์๋ฅผ ` E ` ๋ผ๊ณ ํ์์ ๋ ๊ฐ ๋
ธ๋ ๋ง๋ค edge์ ์๋งํผ ๋ฐ๋ณต์ ํด์ผํ๋ค.
35+ ์๊ฐ ๋ณต์ก๋๋ ` O(V + E) ` ์ด๋ค. ๊ณต๊ฐ ๋ณต์ก๋๋ ` O(V) ` ์ด๋ค.
You canโt perform that action at this time.
0 commit comments