-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclone-graph.py
More file actions
38 lines (35 loc) · 1.09 KB
/
Copy pathclone-graph.py
File metadata and controls
38 lines (35 loc) · 1.09 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
32
33
34
35
36
37
38
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 6 20:56:26 2022
@author: patha
"""
"""
# 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':
def _graph_helper_(arg):
nonlocal exp_dict
if arg is None:
return None
if arg.val in exp_dict.keys():
return exp_dict[arg.val]
retval = Node(arg.val)
exp_dict[arg.val] = retval
neigh_list = deepcopy(arg.neighbors)
if not neigh_list:
return retval
new_neigh_list = [_graph_helper_(x) if x not in exp_dict.keys()
else exp_dict[x.val] for x in neigh_list]
retval.neighbors = new_neigh_list
return retval
if node is None:
return None
if not node.neighbors:
return Node(node.val)
exp_dict = {}
return _graph_helper_(node)