-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0133H. Clone Graph.py
More file actions
31 lines (29 loc) · 1.02 KB
/
0133H. Clone Graph.py
File metadata and controls
31 lines (29 loc) · 1.02 KB
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
#Runtime: 36 ms, faster than 79.84% of Python3 online submissions for Clone Graph.
#Memory Usage: 14.5 MB, less than 83.37% of Python3 online submissions for Clone Graph.
"""
# Definition for a Node.
class Node:
def __init__(self, val = 0, neighbors = None):
self.val = val
self.neighbors = neighbors if neighbors is not None else []
"""
class Solution:
def cloneGraph(self, node: 'Node') -> 'Node':
if not node:
return node
stack = [node]
record = {node:Node(node.val)}
while stack:
c_node = stack.pop(0)
cp_node = record[c_node]
if not cp_node.neighbors:
cp_node.neighbors = []
for i in c_node.neighbors:
if i not in record:
cand = Node(i.val)
cp_node.neighbors += [cand]
record[i] = cand
stack.append(i)
else:
cp_node.neighbors.append(record[i])
return record[node]