#133
Clone Graph
medium· Graphsruns: 0Given a reference of a node in a connected undirected graph, return a deep copy of the graph. Each node contains a value and a list of its neighbors.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 483
class Solution:
def cloneGraph(self, node: "Node | None") -> "Node | None":
if not node:
return None
old_to_new = {}
def dfs(curr: "Node") -> "Node":
if curr in old_to_new:
return old_to_new[curr]
copy = Node(curr.val)
old_to_new[curr] = copy
for neighbor in curr.neighbors:
copy.neighbors.append(dfs(neighbor))
return copy
return dfs(node)
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.