代码随想录算法训练营第十四天| 二叉树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    
相关推荐
小青龙emmm21 分钟前
2025级C语言第四次周测题解
c语言·开发语言·算法
树在风中摇曳31 分钟前
【牛客排序题详解】归并排序 & 快速排序深度解析(含 C 语言完整实现)
c语言·开发语言·算法
minji...34 分钟前
算法---模拟/高精度/枚举
数据结构·c++·算法·高精度·模拟·枚举
代码村新手42 分钟前
数据结构-二叉树
数据结构
姓蔡小朋友43 分钟前
redis GEO数据结构、实现附近商铺功能
数据结构·数据库·redis
Live&&learn1 小时前
数据结构vs 内存结构
数据结构·操作系统·内存结构
buyue__1 小时前
C++实现数据结构——队列和栈
数据结构
执笔论英雄1 小时前
【大模型训练】forward_backward_func返回多个micro batch 损失
开发语言·算法·batch
太理摆烂哥1 小时前
哈希表实现
数据结构·哈希算法·散列表
草莓熊Lotso2 小时前
《算法闯关指南:优选算法--模拟》--41.Z 字形变换,42.外观数列
开发语言·c++·算法