首页 > 其他分享 >代码随想训练营第十六天(Pyhton)| 104.二叉树的最大深度、 111.二叉树的最小深度、222.完全二叉树的节点个数

代码随想训练营第十六天(Pyhton)| 104.二叉树的最大深度、 111.二叉树的最小深度、222.完全二叉树的节点个数

时间:2023-10-28 11:12:22浏览次数:27  
标签:node right return queue 二叉树 深度 root 222 left

104.二叉树的最大深度
1、后续遍历递归法

class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        left_depth = self.maxDepth(root.left) # 左
        right_depth = self.maxDepth(root.right) # 右
        max_depth = 1 + max(left_depth, right_depth) # 中
        return max_depth

2、层序遍历迭代法

class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        depth = 0
        queue = [root]
        while queue:
            n = len(queue)
            depth += 1
            for i in range(n):
                node = queue.pop(0)
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
        return depth

111.二叉树的最小深度
注意和最大深度的区别,什么条件下是最小深度
1、递归法

class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        left = self.minDepth(root.left) # 左
        right = self.minDepth(root.right) # 右

        if root.left is None and root.right:
            return 1 + right

        if root.right is None and root.left:
            return 1 + left

        min_depth = 1 + min(left, right)  # 中
        return min_depth

2、层序遍历

class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        queue = [root]
        depth = 0
        while queue:
            n = len(queue)
            depth += 1
            for i in range(n):
                node = queue.pop(0)
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
                if not node.left and not node.right:
                    return depth
        return depth

222.完全二叉树的节点个数
1、递归法

class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        left = self.countNodes(root.left)
        right = self.countNodes(root.right)
        return 1 + left + right

2、层序遍历

class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        queue = [root]
        count = 0
        while queue:
            n = len(queue)
            count += n
            for _ in range(n):
                node = queue.pop(0)
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
        return count

3、利用完全二叉树的特性

class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        count = 1
        left, right = root.left, root.right
        while left and right:
            count += 1
            left, right = left.left, right.right
        if not left and not right: # 如果同时到底说明是满二叉树,反之则不是
            return 2 ** count - 1
        else:
            return 1 + self.countNodes(root.left) + self.countNodes(root.right)

标签:node,right,return,queue,二叉树,深度,root,222,left
From: https://www.cnblogs.com/yixff/p/17788118.html

相关文章

  • AQS是什么?AbstractQueuedSynchronizer之AQS原理及源码深度分析
    文章目录一、AQS概述1、什么是AQS2、技术解释3、基本原理4、AQS为什么这么重要二、AQS数据结构1、AQS的结构2、ReentrantLock与AbstractQueuedSynchronizer3、AQS的state变量4、AQS的队列5、AQS的Node(1)Node的waitStatus(2)属性说明三、ReentrantLock的lock方法分析AQS源码1、类图2、......
  • #yyds干货盘点#深度讲解React Props
    一、props的介绍当React遇到的元素是用户自定义的组件,它会将JSX属性作为单个对象传递给该组件,这个对象称之为“props”。函数声明的组件,会接受一个props形参,获取属性传递的参数functionComponentA(props){return<div>我是组件B:{props.value}</div>}如果函数组件需要prop......
  • 深度学习(统计模型参数量)
    统计模型参数量,方便判断不同模型大小:importtorchimporttorch.nnasnn#自定义AlexNet模型classAlexNet(nn.Module):def__init__(self):super(AlexNet,self).__init__()self.conv1=nn.Conv2d(1,96,kernel_size=11,stride=4)self.......
  • C++从std::vector<int>类型数据创建二叉树
    背景在和chatGPT的日常代码交流中,这位“老师”总能给出不不少好代码,以下就是C++从std::vector类型数据创建二叉树的完整代码段:TreeNode*createBinaryTree(conststd::vector<int>&nodes,intindex){if(index>=nodes.size()||nodes[index]==-1){retu......
  • 一文搞懂深度信念网络!DBN概念介绍与Pytorch实战
    本文深入探讨了深度信念网络DBN的核心概念、结构、Pytorch实战,分析其在深度学习网络中的定位、潜力与应用场景。关注TechLead,分享AI与云服务技术的全维度知识。作者拥有10+年互联网服务架构、AI产品研发经验、团队管理经验,同济本复旦硕,复旦机器人智能实验室成员,阿里云认证的资......
  • [Leetcode] 0104. 二叉树的最大深度
    104.二叉树的最大深度题目描述给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树[3,9,20,null,null,15,7],3/\920/\157返回它的最大深度 3。......
  • 基于Googlenet深度学习网络的信号调制类型识别matlab仿真
    1.算法运行效果图预览   2.算法运行软件版本MATLAB2022a 3.算法理论概述      信号调制类型识别是在无线通信和无线电频谱监测中的一个重要任务。不同信号调制类型具有不同的频谱特征,深度学习方法在信号调制类型识别中取得了显著的成果。 3.1深度学习与......
  • 94.二叉树的中序遍历
    1.题目介绍给定一个二叉树的根节点root,返回它的中序遍历。示例1:输入:root=[1,null,2,3]输出:[1,3,2]示例2:输入:root=[]输出:[]示例3:输入:root=[1]输出:[1]提示:树中节点数目在范围[0,100]内-100<=Node.val<=1002.题解2.1递归首先我们需......
  • 深度解读MediaBox SDKs如何实现技术架构升级
    本专栏将分享阿里云视频云MediaBox系列技术文章,深度剖析音视频开发利器的技术架构、技术性能、开发能效和最佳实践,一起开启音视频的开发之旅。本文为MediaBox技术架构篇,重点从音视频终端SDK的技术架构、优化设计、架构优势等方面,介绍MediaBoxSDKs如何实现技术架构升级。善师|作者......
  • 深度解读MediaBox SDKs如何实现技术架构升级
    本专栏将分享阿里云视频云MediaBox系列技术文章,深度剖析音视频开发利器的技术架构、技术性能、开发能效和最佳实践,一起开启音视频的开发之旅。本文为MediaBox技术架构篇,重点从音视频终端SDK的技术架构、优化设计、架构优势等方面,介绍MediaBoxSDKs如何实现技术架构升级。:::hlj......