133. Clone Graph
https://leetcode.com/problems/clone-graph
Python
DFS
class Solution:
def cloneGraph(self, node: 'Node') -> 'Node':
seem = dict()
def dfs(node):
if not node:
return
if node.val in seem:
return seem[node.val]
new_node = Node(
val=node.val,
neighbors=[]
)
seem[node.val] = new_node
for neighbor in node.neighbors:
new_node.neighbors.append(dfs(neighbor))
return new_node
return dfs(node)