Skip to content

Commit 2201d91

Browse files
Solve : Clone Graph
1 parent cb66bbf commit 2201d91

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

clone-graph/printjin-gmailcom.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)