代码随想录算法训练营第十四天| 二叉树2

226. 翻转二叉树

这道题目 一些做过的同学 理解的也不够深入,建议大家先看我的视频讲解,无论做过没做过,都会有很大收获。
题目链接/文章讲解/视频讲解:代码随想录

状态:知道要用递归,但不确定递归怎么写

递归方法如下:

python 复制代码
# 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 not root:
            return None
        root.left,root.right=root.right,root.left
        self.invertTree(root.left)
        self.invertTree(root.right)
        return root

101. 对称二叉树

先看视频讲解,会更容易一些。
题目链接/文章讲解/视频讲解:代码随想录

状态:需要看讲解

另外写判断的函数,进行递归,分类情况

python 复制代码
# 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:
        if not root:
            return True
        return self.compare(root.left,root.right)

    def compare(self,left,right):
        if left!=None and right==None: return False
        elif left==None and right!=None: return False
        elif left==None and right==None: return True
        elif left.val!=right.val: return False
        
        outside=self.compare(left.left,right.right)
        inside=self.compare(left.right,right.left)
        isSame=outside and inside
        return isSame

104. 二叉树的最大深度

什么是深度,什么是高度,如何求深度,如何求高度,这里有关系到二叉树的遍历方式。
大家 要先看视频讲解,就知道以上我说的内容了,很多录友刷过这道题,但理解的还不够。

题目链接/文章讲解/视频讲解: 代码随想录

状态:需要看讲解

计算左右子树的最大深度

python 复制代码
# 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 maxDepth(self, root: Optional[TreeNode]) -> int:
        return self.depth(root)
    def depth(self,node):
        if not node:
            return 0
        leftdepth=self.depth(node.left)
        rightdept=self.depth(node.right)
        length=1+max(leftdepth,rightdept)
        return length

111. 二叉树的最小深度

先看视频讲解,和最大深度 看似差不多,其实 差距还挺大,有坑。
题目链接/文章讲解/视频讲解:代码随想录

状态:叶子结点最后应该分类分析

对左子树空右子树非空和左子树非空右子树空的情况分类讨论

python 复制代码
# 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 minDepth(self, root: Optional[TreeNode]) -> int:
        return self.depth(root)
    def depth(self,node):
        if not node:
            return 0
        leftdepth=self.depth(node.left)
        rightdepth=self.depth(node.right)
        if node.left==None and node.right!=None:
            return 1+rightdepth
        if node.left!=None and node.right==None:
            return 1+leftdepth
        length=1+min(leftdepth,rightdepth)
        return length    
相关推荐
前端小白在前进1 天前
⭐力扣刷题:螺旋矩阵
算法·leetcode·矩阵
老赵聊算法、大模型备案1 天前
北京市生成式人工智能服务已备案信息公告(2025年12月11日)
人工智能·算法·安全·aigc
CoderYanger1 天前
C.滑动窗口-求子数组个数-越长越合法——2799. 统计完全子数组的数目
java·c语言·开发语言·数据结构·算法·leetcode·职场和发展
厕所博士1 天前
红黑树原理前置理解—— 2-3 树
算法·2-3树·红黑树原理理解前置
萌>__<新1 天前
力扣打卡每日一题————除自身外所有元素的乘积
数据结构·算法
xu_yule1 天前
算法基础—搜索(2)【记忆化搜索+BFS+01BFS+Floodfill]
数据结构·算法
s09071361 天前
Xilinx FPGA使用 FIR IP 核做匹配滤波时如何减少DSP使用量
算法·fpga开发·xilinx·ip core·fir滤波
老马啸西风1 天前
成熟企业级技术平台-10-跳板机 / 堡垒机(Bastion Host)详解
人工智能·深度学习·算法·职场和发展
子夜江寒1 天前
逻辑回归简介
算法·机器学习·逻辑回归
软件算法开发1 天前
基于ACO蚁群优化算法的多车辆含时间窗VRPTW问题求解matlab仿真
算法·matlab·aco·vrptw·蚁群优化·多车辆·时间窗