133. Clone Graph
202402072037
tags: #graph
class Solution:
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
def clone(node):
if node in map:
return map[node]
newNode = Node(node.val)
map[node] = newNode
for neighbor in node.neighbors:
newNode.neighbors.append(clone(neighbor))
return newNode
map = {}
return clone(node) if node else None