LeetcodeDFS知识点总结:求树的最⼤深度, Easy
class Solution(object):
def maxDepth(lf, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root: return0
return max(lf.maxDepth(root.left),lf.maxDepth(root.right))+1
:求最⼤岛屿⾯积,Easy
dfs+记忆化搜索
class Solution(object):
def maxAreaOfIsland(lf, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
m = len(grid)
if m == 0: return0
n = len(grid[0])
disvisited = [[True for j in range(n)] for i in range(m)]
def dfs(ii,jj):
ret = 0
dx = [0,0,-1,1]
dy = [1,-1,0,0]
for k in range(4):
x = ii + dx[k]
y = jj + dy[k]
雨痕if x>=0and x<m and y>=0and y<n and disvisited[x][y] and grid[x][y]: disvisited[x][y] = Fal
ret = ret+1+dfs(x,y)
return ret
maxret = 0
for i in range(m):
for j in range(n):
if disvisited[i][j] and grid[i][j]:
disvisited[i][j] = Fal
cnt = 1 + dfs(i,j)
maxret = max(maxret,cnt)
return maxret
霁月难逢
qq标签怎么设置:求指定id和其所有下属的总主要性,Easy
class Solution(object):
def getImportance(lf, employees, id):
"""
:type employees: Employee
:
type id: int
:rtype: int
"""
emp = dict()
感动中国人物评选visited = dict()
for employee in employees:
emp[employee.id] = employee
def dfs(idx):
if idx in visited:
return0
visited[idx] = True
ret=emp[idx].importance
for sub in emp[idx].subordinates:
ret+=dfs(sub)
return ret
return dfs(id)
:从出发点(sr,sc)开始,与之相连的有相同像素的点都换成newColor, Easy
class Solution(object):
def floodFill(lf, image, sr, sc, newColor):
"""
:type image: List[List[int]]
:type sr: int
:
type sc: int
:type newColor: int
:rtype: List[List[int]]
"""
m = len(image)
if m == 0: return image
n = len(image[0])
disvisited = [[True for j in range(n)] for i in range(m)]
rawColor = image[sr][sc]
def dfs(xx,yy):
disvisited[xx][yy] = Fal
image[xx][yy] = newColor
dx = [0,0,-1,1]
dy = [1,-1,0,0]
for k in range(4):
x = xx+dx[k]
y = yy+dy[k]
if x>=0and x<m and y>=0and y<n and disvisited[x][y] and image[x][y]==rawColor: dfs(x,y)
dfs(sr,sc)
return image
:判断两个树是否相同,Easy
# lf.left = None
# lf.right = None
class Solution(object):
def isSameTree(lf, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if p is None and q is None:
return True
elif p is None or q is None:
return Fal
el:
if p.val==q.val:
if lf.isSameTree(q.left,p.left):
return lf.isSameTree(p.right,q.right)
return Fal
:把⼀个有序列表转化为⼆叉搜索树,Easy
根据⼆叉搜索树的性质,左树上的值<;根结点的值<;右树上的值
因此可以取 nums/2 位置上的值作为根结点的值,然后再左右递归调⽤即可
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(lf, x):恨的近义词
# lf.val = x
# lf.left = None
# lf.right = None
class Solution(object):
def sortedArrayToBST(lf, nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
length = len(nums)
if length==0:
return None
if length == 1:
return TreeNode(nums[0])
root = TreeNode(nums[length/2])
root.left = lf.sortedArrayToBST(nums[:(length/2)])
root.right =lf.sortedArrayToBST(nums[(length/2+1):])
return root
:判断树是否是对称树,Easy
# lf.left = None
# lf.right = None
class Solution(object):
def isSymmetric(lf, root):
"""
:type root: TreeNode
:rtype: bool
"""
if root==None:
return True
return lf.symetric(root.left,root.right)
def symetric(lf,root_a,root_b):
if root_a==None and root_b==None:
return True
if root_a==None or root_b==None:
return Fal
if root_a.val !=root_b.val:
return Fal
return lf.symetric(root_a.left,root_b.right) and lf.symetric(root_a.right,root_b.left):打印⼆叉树的所有路径,Easy
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(lf, x):
# lf.val = x
# lf.left = None
# lf.right = None
class Solution(object):
def binaryTreePaths(lf, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
ret = []
if root == None:
return ret
cur_val = root.val
if root.left:
ret += lf.binaryTreePaths(root.left)
if root.right:
ret += lf.binaryTreePaths(root.right)
if ret == []:
return [str(cur_val)]
el:
for i in range(len(ret)):
ret[i] = str(cur_val)+'->'+ret[i]
return ret
:判断⼆叉树是否是平衡⼆叉树,Easy
左⼦树是否平衡+右⼦树是否平衡+左右⼦树的⾼度差<=1
# lf.left = None
# lf.right = None
class Solution(object):
def height(lf,root):热门小说排行榜前十名
if root == None:
return0
if root.left == None and root.right == None:
return1
return max(lf.height(root.left),lf.height(root.right))+1
酸菜豆腐鱼def isBalanced(lf, root):
"""
:type root: TreeNode
:rtype: bool
"""
if root ==None:
return True
return lf.isBalanced(root.left) and lf.isBalanced(root.right) and abs(lf.height(root.left)-lf.height(root.right))<=1:求⼆叉树的最⼩深度,Easy
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(lf, x):
# lf.val = x
# lf.left = None
# lf.right = None
class Solution(object):
少先队员知识def minDepth(lf, root):
"""
:type root: TreeNode
:rtype: int
"""
if root == None:
return0
if root.left == None and root.right !=None:
return lf.minDepth(root.right)+1
if root.right == None and root.left !=None:
return lf.minDepth(root.left)+1
return min(lf.minDepth(root.left),lf.minDepth(root.right))+1
:求⼆叉树最后⼀⾏最左边的值,Medium
题外话,这题BFS的速度⽐DFS快很多,我两个都贴出来了