-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloneGraph.py
More file actions
25 lines (18 loc) · 743 Bytes
/
Copy pathcloneGraph.py
File metadata and controls
25 lines (18 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from collections import deque
class Node:
def __init__(self, val=0, neighbors=None):
self.val = val
self.neighbors = [] if neighbors is None else neighbors
class Solution:
def cloneGraph(self, node: Node | None) -> Node | None:
if not node: return None
queue = deque([node])
orig_to_new = {node: Node(node.val)}
while queue:
curr_node = queue.popleft()
for neighbor in curr_node.neighbors:
if neighbor not in orig_to_new:
queue.append(neighbor)
orig_to_new[neighbor] = Node(neighbor.val)
orig_to_new[curr_node].neighbors.append(orig_to_new[neighbor])
return orig_to_new[node]