代码随想录算法训练营第十四天| 二叉树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    
相关推荐
超的小宝贝15 分钟前
数据结构算法(C语言)
c语言·数据结构·算法
凤年徐2 小时前
【数据结构初阶】单链表
c语言·开发语言·数据结构·c++·经验分享·笔记·链表
木子.李3476 小时前
排序算法总结(C++)
c++·算法·排序算法
闪电麦坤957 小时前
数据结构:递归的种类(Types of Recursion)
数据结构·算法
小熊猫写算法er8 小时前
终极数据结构详解:从理论到实践
数据结构
Gyoku Mint8 小时前
机器学习×第二卷:概念下篇——她不再只是模仿,而是开始决定怎么靠近你
人工智能·python·算法·机器学习·pandas·ai编程·matplotlib
纪元A梦8 小时前
分布式拜占庭容错算法——PBFT算法深度解析
java·分布式·算法
px不是xp8 小时前
山东大学算法设计与分析复习笔记
笔记·算法·贪心算法·动态规划·图搜索算法
-qOVOp-9 小时前
408第一季 - 数据结构 - 栈与队列的应用
数据结构