题目: 【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
推荐阅读
- 【C】题目|【C语言】题集 of ⑥
- 数据结构与算法|【算法】力扣第 266场周赛
- leetcode|今天开始记录自己的力扣之路
- Python|Python 每日一练 二分查找 搜索旋转排序数组 详解
- 【LeetCode】28.实现strstr() (KMP超详细讲解,sunday解法等五种方法,java实现)
- LeetCode-35-搜索插入位置-C语言
- 题目|C. Ayoub and Lost Array(思维dp)
- leetcode python28.实现strStr()35. 搜索插入位置
- Leetcode Permutation I & II
- python|leetcode Longest Substring with At Most Two Distinct Characters 滑动窗口法