class Solution: def cloneGraph_2(self, node, relation_dict): if node in relation_dict: return tmp = Node(val=node.val) relation_dict[node] = tmp for i in node.neighbors: self.cloneGraph_2(i, relation_dict) tmp.neighbors.append(relation_dict[i]) def cloneGraph(self, node: 'Node') -> 'Node': if node is None: return None relation_dict = {} self.cloneGraph_2(node, relation_dict) return relation_dict[node]