leetcode|133.克隆图 python3

题目: 【leetcode|133.克隆图 python3】leetcode|133.克隆图 python3
文章图片

思路:

  • 深度遍历法
代码:
""" # Definition for a Node. class Node: def __init__(self, val, neighbors): self.val = val self.neighbors = neighbors """ class Solution: def cloneGraph(self, node: 'Node') -> 'Node': self.visited = {} return self.getNode(node)def getNode(self, node): if node.val in self.visited: return self.visited[node.val] copy_node = Node(node.val, []) self.visited[node.val] = copy_node for i in node.neighbors: copy_node.neighbors.append(self.getNode(i))return copy_node

    推荐阅读