forked from SocialfiPanda/Leetcode-Py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClone Graph.py
More file actions
19 lines (19 loc) · 756 Bytes
/
Clone Graph.py
File metadata and controls
19 lines (19 loc) · 756 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
def cloneGraph(self, node):
if node == None:
return None
start = UndirectedGraphNode(node.label)
map, current = {node: start}, [node]
while len(current) > 0:
next = []
for x in current:
for neighbor in x.neighbors:
if neighbor not in map:
neighbor_copy = UndirectedGraphNode(neighbor.label)
next.append(neighbor)
map[x].neighbors.append(neighbor_copy)
map[neighbor] = neighbor_copy
else:
map[x].neighbors.append(map[neighbor])
current = next
return start