首页 > 其他分享 >111. Minimum Depth of Binary Tree刷题笔记

111. Minimum Depth of Binary Tree刷题笔记

时间:2023-05-26 22:05:09浏览次数:29  
标签:Binary right return minD self Tree Depth root left


需要考虑这种情况:

111. Minimum Depth of Binary Tree刷题笔记_深度优先

# 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:
        if not root:
            return 0
        if not root.left and not root.right:
            return 1
        minD = 10**9
        if root.left:
            minD = min(self.minDepth(root.left),minD)
        if root.right:
            minD = min(self.minDepth(root.right),minD)
        return minD+1

111. Minimum Depth of Binary Tree刷题笔记_算法_02


标签:Binary,right,return,minD,self,Tree,Depth,root,left
From: https://blog.51cto.com/u_16131692/6359366

相关文章

  • 前端树形结构图treeShapeStruct,可拖拽移动,点击展开收缩,无限添加子集
    快速实现树形结构图,可拖拽移动,点击展开收缩,无限添加子集;下载完整代码请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=12650效果图如下:  实现代码如下:#treeShapeStruct树形结构图,可拖拽移动,点击展开收缩,无限添加子集使用方法####HTML代码部分```......
  • 前端树形结构图组件 tree组件,可拖拽移动,点击展开收缩,无限添加子集
    快速实现树形结构图组件tree组件,可拖拽移动,点击展开收缩,无限添加子集;下载完整代码请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=12650效果图如下:  实现代码如下:#treeShapeStruct树形结构图,可拖拽移动,点击展开收缩,无限添加子集使用方法####HTM......
  • vue <treeSelect标签中树形选择返回非必填调整以及清内存需手动清理
    绑定必须相同 校验规则需trigger我这边用的是change(数据发生改变时发生变化触发),原来用的blur(失去焦点时触发)如上解决了我此次第一次选择完数据还报非必填问题 清理缓存手动清理了一下dataform中的gcl的数据要不然新增完事后还会返回数据 ......
  • iOS MachineLearning 系列(18)—— PoseNet,DeeplabV3与FCRN-DepthPrediction模型
    iOSMachineLearning系列(18)——PoseNet,DeeplabV3与FCRN-DepthPrediction模型本篇文章将再介绍三个官方的CoreML模型:PoseNet,DeeplabV3和FCRN-DepthPrediction。PoseNet是人体姿势分析模型,可以识别图片中的人体部分,然后以17个基准点来描述人体的姿势。关于人体姿势的识别,其实Vision......
  • JS 树形数据 Tree的通用方法
    点击查看代码/***@description查找包含自身节点的父代节点*@paramlist列表数据*@paramid节点id*@parampid节点的父id*/exportfunctionlistToTree(list,id,pid){list.forEach((node)=>{constpNdoe=list.find((row)=>row[id]===nod......
  • 1066 Root of AVL Tree
    题目:AnAVLtreeisaself-balancingbinarysearchtree.InanAVLtree,theheightsofthetwochildsubtreesofanynodedifferbyatmostone;ifatanytimetheydifferbymorethanone,rebalancingisdonetorestorethisproperty.Figures1-4illustr......
  • CF1819C The Fox and the Complete Tree Traversal
    \(\color{purple}\text{TheFoxandtheCompleteTreeTraversal}\)比较有意思的一题。先考虑一个序列的权值。对长度为\(len\)的序列排序,价值为\(len-1\),那么有时候如果后面的元素很大,前面的很小:321300200100我们可以将序列切为\([1,3]\),和\([4,6]\)两部分分别......
  • sourceTree环境配置
    安装并配置完成git1、在gitbrashhere中允许命令ssh-keygen-ted25519-C"[email protected]" 按照提示完成三次回车,在用户目录下默认生成文件夹.ssh,打开可以找到id_rsa.pub文件,获取到你的publickeycat~/.ssh/id_ed25519.pub复制生成的SSH,通过仓库主页「管理」->「部署......
  • 修改arm板开机logo,ppm转换需要用ascii而不是rawbits binary
    网上在线转ppm格式不好用,转出来的是rawbits的二进制格式,PPM编码(ASCII或binary),关于图片格式编码参见此处我需要ascii编码sudoapt-getinstallnetpbm        $bmptoppmpic.bmp>temp1.ppm//生成ppm        $ppmquant224temp1.ppm>temp2.ppm//转换成2......
  • 1110 Complete Binary Tree(附测试点2,3,4,6分析)
    题目:Givenatree,youaresupposedtotellifitisacompletebinarytree.InputSpecification:Eachinputfilecontainsonetestcase.Foreachcase,thefirstlinegivesapositiveinteger N (≤20)whichisthetotalnumberofnodesinthetree--andh......