Tech & SaaS
Senior
programming
LeetCode #133 - Clone Graph: Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph. Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors. Test the implementation with the following example: Input: 1 -- 2 -- 3, Output: A deep copy of the graph.
```
def cloneGraph(node: 'Node') -> 'Node':
if not node:
return None
clone = {node: Node(node.val)}
queue = deque([node])
while queue:
curr = queue.popleft()
for neighbor in curr.neighbors:
if neighbor not in clone:
clone[neighbor] = Node(neighbor.val)
queue.append(neighbor)
clone[curr].neighbors.append(clone[neighbor])
return clone[node]
```
Trusted by 100+ professionals preparing for interviews
Trusted by 100+ professionals
50+ Company Question Banks
5+ Supported Languages
Practice More Questions Like This
Generate unlimited interview questions with structured answers, code runner, and AI-powered walkthroughs.
Get Started Free
More Tech & SaaS Interview Prep
How do you ensure high code quality in your projects, and what specific practices do you implement to maintain it?
Tech & SaaS · Senior
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.