代码随想录算法训练营第十四天| 二叉树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    
相关推荐
前端炒粉3 小时前
18.矩阵置零(原地算法)
javascript·线性代数·算法·矩阵
im_AMBER3 小时前
数据结构 09 二叉树作业
数据结构·笔记·学习
暴风鱼划水4 小时前
三维重建【0-D】3D Gaussian Splatting:相机标定原理与步骤
算法·3d
l1t5 小时前
利用DeepSeek修改数据结构提升求解集合程序效率
数据结构·python·deepseek
mount_myj6 小时前
敏感信息屏蔽(一)【java】
java·算法·极课堂
先做个垃圾出来………7 小时前
偏移量解释
数据结构·算法
FanXing_zl7 小时前
基于整数MCU的FOC控制定标策略深度解析
单片机·嵌入式硬件·mcu·算法·定点运算·q15
立志成为大牛的小牛7 小时前
数据结构——三十三、Dijkstra算法(王道408)
数据结构·笔记·学习·考研·算法·图论
地平线开发者8 小时前
mul 与 reduce_sum 的优化实例
算法·自动驾驶