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 cb66bbf commit 2201d91Copy full SHA for 2201d91
clone-graph/printjin-gmailcom.py
@@ -0,0 +1,15 @@
1
+from typing import Optional
2
+class Solution:
3
+ def cloneGraph(self, node):
4
+ if not node:
5
+ return None
6
+ old_to_new = {}
7
+ def dfs(n):
8
+ if n in old_to_new:
9
+ return old_to_new[n]
10
+ copy = Node(n.val)
11
+ old_to_new[n] = copy
12
+ for neighbor in n.neighbors:
13
+ copy.neighbors.append(dfs(neighbor))
14
+ return copy
15
+ return dfs(node)
0 commit comments