133. Clone Graph

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]

 

posted @ 2021-08-08 20:14  茫茫碧落  阅读(20)  评论(0编辑  收藏  举报