代码随想录算法训练营第十四天| 二叉树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    
相关推荐
MicroTech20253 分钟前
微算法科技(NASDAQ: MLGO)采用量子相位估计(QPE)方法,增强量子神经网络训练
大数据·算法·量子计算
星梦清河7 分钟前
宋红康 JVM 笔记 Day15|垃圾回收相关算法
jvm·笔记·算法
货拉拉技术16 分钟前
揭秘语音交互的核心技术
算法
矛取矛求1 小时前
日期类的实现
开发语言·c++·算法
在下雨5991 小时前
项目讲解1
开发语言·数据结构·c++·算法·单例模式
Jayyih1 小时前
嵌入式系统学习Day36(简单的网页制作)
学习·算法
今后1231 小时前
【数据结构】栈详解
数据结构·
脑洞代码2 小时前
20250909的学习笔记
算法
Christo32 小时前
TFS-2003《A Contribution to Convergence Theory of Fuzzy c-Means and Derivatives》
人工智能·算法·机器学习
黑菜钟2 小时前
代码随想录第七天|● 454.四数相加II ● 383. 赎金信 ● 15. 三数之和 18.四数之和
c++·算法·leetcode