We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 43f0c39 commit 9db56f3Copy full SHA for 9db56f3
clone-graph/kayden.py
@@ -0,0 +1,28 @@
1
+from typing import Optional
2
+from collections import deque
3
+
4
5
+class Solution:
6
+ # 시간복잡도: O(N) node 개수: N
7
+ # 공간복잡도: O(N)
8
+ def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
9
+ if node:
10
+ visited = {}
11
+ copy = Node(val=node.val)
12
+ visited[copy.val] = copy
13
+ q = deque()
14
+ q.append((copy, node))
15
16
+ while q:
17
+ cur, node = q.popleft()
18
19
+ for idx, next_node in enumerate(node.neighbors):
20
+ if next_node.val not in visited:
21
+ new = Node(val=next_node.val)
22
+ visited[new.val] = new
23
+ q.append((new, next_node))
24
+ cur.neighbors.append(visited[next_node.val])
25
26
+ return copy
27
28
+ return node
0 commit comments