一、学习视频
如何灵活运用递归?【基础算法精讲 10】_哔哩哔哩_bilibili
二、跟练代码
1. 100. 相同的树
递归
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:# 递归if p is None and q is None:return Trueelif p is None or q is None:return Falseleft = self.isSameTree(p.left,q.left)right = self.isSameTree(p.right,q.right)return left and right and p.val == q.val
学习一下灵神的写法,来自视频。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:# 递归# if p is None and q is None:# return True# elif p is None or q is None:# return Falseif p is None or q is None:return p is qleft = self.isSameTree(p.left,q.left)right = self.isSameTree(p.right,q.right)return left and right and p.val == q.val
2.101. 对称二叉树
递归
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:def isSymmetric(self, root: Optional[TreeNode]) -> bool:# 递归# 含有根节点# 判断根节点的左右两子树def f(left: Optional[TreeNode], right: Optional[TreeNode]) -> bool:if left is None or right is None:return left is rightreturn f(left.left,right.right) and f(left.right,right.left) and left.val == right.val# #根节点为空时是满足的,此处含根节点可以省去# if root is None: # return Truereturn f(root.left,root.right)
3.110. 平衡二叉树
递归。不会,来自视频代码。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:def isBalanced(self, root: Optional[TreeNode]) -> bool:# 递归# 计算高度,非负数# 用-1表示不是平衡二叉树def f(node) -> int:if node is None:return 0# left_h = f(node.left)# right_h = f(node.right)# if left_h == -1 or right_h == -1 or abs(left_h - right_h) > 1:# return -1# return max(left_h, right_h) + 1# 少递归一些left_h = f(node.left)if left_h == -1:return -1right_h = f(node.right)if right_h == -1 or abs(left_h - right_h) > 1:return -1return max(left_h, right_h) + 1return f(root) != -1
4.199. 二叉树的右视图
递归
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:def rightSideView(self, root: Optional[TreeNode]) -> List[int]:# 递归# 先右后左遍历,当高度大于minHeight时,才会露出来ans = []minHeight = 0def f(node,height):nonlocal minHeightif node is None:return if height > minHeight:ans.append(node.val)minHeight += 1right = f(node.right,height + 1)left = f(node.left,height + 1)return f(root,1)return ans
三、课后作业
1.226. 翻转二叉树
递归
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:# 递归if root is None:return Noneleft = self.invertTree(root.left) #左子树right = self.invertTree(root.right) #右子树root.left = right #左右互换root.right = leftreturn root
2.1026. 节点与其祖先之间的最大差值
不会,全都来自灵神题解(. - 力扣(LeetCode)),学习学习。
(1)递
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:def maxAncestorDiff(self, root: Optional[TreeNode]) -> int:# 递v = -1def f(node,mx,mn):nonlocal vif node is None:return #不一定要有返回值mx = max(mx,node.val)mn = min(mn,node.val)v = max(v, node.val - mn, mx - node.val) #更新f(node.left,mx,mn)f(node.right,mx,mn)f(root, root.val, root.val) #根节点存在return v
(2)递的优化
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:def maxAncestorDiff(self, root: Optional[TreeNode]) -> int:# 递的优化v = -1def f(node,mx,mn):if node is None:nonlocal vv = max(v, mx - mn) #到终点更新returnmx = max(mx,node.val)mn = min(mn,node.val)f(node.left,mx,mn)f(node.right,mx,mn)f(root, root.val, root.val) #根节点存在return v
(3)归
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:def maxAncestorDiff(self, root: Optional[TreeNode]) -> int:# 归v = -1def f(node):if node is None:return inf, -inf # min maxmn = mx = node.vallmin, lmax = f(node.left)rmin, rmax = f(node.right)mn = min(mn, lmin, rmin)mx = max(mx, lmax, rmax)nonlocal v# 每一次都更新vv = max(v, node.val - mn, mx - node.val)return mn, mxf(root)return v
递可以不用每次都更新v值,因为从上到下能确保最大小值就是该条路径的,而没有受其他路径影响;而归每次都需更新v值,一个节点有左右两边两条路径到达。
(未完待续)
感谢你看到这里!一起加油吧!