-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloneGraph.py
34 lines (34 loc) · 947 Bytes
/
CloneGraph.py
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
26
27
28
29
30
31
32
33
34
"""
# Definition for a Node.
class Node:
def __init__(self, val = 0, neighbors = []):
self.val = val
self.neighbors = neighbors
"""
class Solution:
def cloneGraph(self, node: 'Node') -> 'Node':
if not node:
return None
g = [None]*101
ma = 0
queue = [node]
visited = set()
while queue:
curr = queue.pop(0)
if curr.val in visited:
continue
else:
visited.add(curr.val)
childs = []
for i in curr.neighbors:
childs.append(i.val)
g[curr.val] = childs
ma = max(ma,curr.val)
queue += curr.neighbors
graph = [None]
for i in range(1,ma+1):
graph.append(Node(val=i))
for i in range(1,ma+1):
for j in g[i]:
graph[i].neighbors.append(graph[j])
return graph[1]