[LeetCode] 111. Minimum Depth of Binary Tree

版图提取的spice网表中参数含义及二级效应STI/WPE 寄生电阻、寄生电容可以从版图提出的.sp文件中看, ad area of drain 面积 as area of source pd perimeter of drain 周长,PS PD周长都不计算与栅极接触的部分 ps perimeter of source nrd 漏扩散区等效方块数 nrs 源扩散区等效方块数 sa ... 阅读详情

原题链接: https://leetcode.com/problems/minimum-depth-of-binary-tree/

1. 题目介绍

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Note: A leaf is a node with no children.
给定一个二叉树,返回它最小的深度。
最小的深度是指从头节点到叶子节点最短的距离。
叶子节点是指没有子节点的节点
Example:
Given binary tree [3,9,20,null,null,15,7],
return its minimum depth = 2.
在这里插入图片描述

2. 解题思路

本题和 104. Maximum Depth of Binary Tree 非常相似。一个是求树的最大深度,一个是求树的最小深度。方法都是一样的,那就是深度优先搜索+递归。

唯一不同之处在于,在求最小深度时,需要考虑只有左子树或者只有右子树的情况。比如测试样例 [1,2] , 1是根节点,2是左子树,没有右子树。这是返回的结果应该2,而不是1.

实现代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int minDepth(TreeNode root) {
        return helper(0,root);
    }
    public int helper(int depth,TreeNode root){
        if(root == null){
            return depth;
        }
        if(root.left == null && root.right == null){
            return depth + 1;
        }
        
        int l = (root.left  == null ? Integer.MAX_VALUE : helper(depth+1,root.left) );
        int r = (root.right == null ? Integer.MAX_VALUE : helper(depth+1,root.right) );
        
        return Math.min(l, r);
    }
}
Arduino平台软硬件原理及使用——Arduino Uno板卡的使用 Arduino Uno板卡的基本使用 阅读详情

相关推荐

SAP 分期付款,付款条件的要件定义方法

以上内容做好后,可以在F-02或者F-60里看设置后的结果。找到相关路径选择【维护支付条件】点击进入后,选择【新条目】再设置一个总的支付规则,注意这里【付款条款】一定要选择。例:默认付款条件SP00,改成刚定义好的0028即可。将刚才基础创建的付款条件,以以下方式录入,注意。028A- 30天到期,过账日期基础。028B- 60天到期,过账日期基础。028C- 90天到期,过账日期基础。目的:新建支付条款(本次以。的选择,然后保存后,完成。,【基准日期缺省值】选择。基础创建好了以后,选择【

晴天的博客分享技术知识 2948

LeetCode力扣 111. 二叉树的最小深度 Minimum Depth of Binary Tree 题解代码 JavaScript

问题https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/ 练习使用JavaScript解答 /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root .

Coding home - 漂流瓶jz 480

微信小程序利用Echarts加载中国地图的步骤

微信小程序如何利用Echarts加载地图?可按照如下四个步骤进行。(阅读时间大概3分钟)。

成都都成 5299

LeetCode 104. Maximum Depth of Binary Tree和111. Minimum Depth of Binary Tree 递归

104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

老夫编代码就是一把梭 596

保姆级教程:在RK3576上配置USB网卡从零到外网访问(附udev规则详解)

本文提供了在RK3576开发板上配置USB网卡的完整教程,涵盖从内核驱动、udev规则到网络路由的全流程。重点讲解了如何利用USB Gadget功能实现高速网络连接,并构建稳定的算力集群,确保多设备间通信及外网访问。

grass的博客 473

AI赋能运筹优化:从参数生成到智能求解的工程实践

运筹学通过线性规划、整数规划等数学模型,为供应链、物流、生产排程等复杂决策问题寻找最优解,其核心价值在于提升资源利用效率与决策科学性。传统方法依赖精确的输入参数与专家经验,面临数据不准、建模门槛高、求解调优难等挑战。人工智能技术的引入,正从原理层面重塑这一流程:通过深度学习模型生成更贴合现实的动态参数(如需求分布、行驶时间),利用自然语言处理降低建模门槛,并借助强化学习对求解器进行智能参数调优。这种AI与运筹学的融合,在工程实践中显著提升了优化系统的自适应能力与求解效率,使其在应对实时交通、柔性生产调度等动

weixin_26913055的博客 607

LeetCode: 111. Minimum Depth of Binary Tree

LeetCode: 111. Minimum Depth of Binary Tree 题目描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest...

杨领well的专栏 688

MFC工具栏增加EditBox和Button

1. 在资源视图中增加EditBox, Button控件,ID分别为ID_EDIT_SEED, ID_BTN_SEED 2. 在CMainFrame中增加成员变量 CMFCToolBarEditBoxButton *m_editSeed; CMFCToolBarButton *m_btnSeed; 3. 在CMainFrame::OnCreate中创建控件 m_editSeed = new CMFCToolBarEditBoxButton(ID_EDIT_SEED, GetCmdMgr()-&.

学习是为了探索这个世界的本质 810

[LeetCode 111] Minimum Depth of Binary Tree

题目链接:minimum-depth-of-binary-tree /** * Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest

瓦尔登湖 1394

论文工具——写论文好用的绘图工具(甘特图+流程图+网络模型图+泳道图)

* 在写HiFi-GAN论文的代码阅读过程中,我发现仅仅通过文字来描述网络结构,不够详细,所以想绘制出网络结构图,更加具象化的描述出网络结构。想去花了,才发现之前都是通过在线绘图网站画的,费时费力,所以想找一些专门的网站化出比较规范并且高质量的图。一方面是为了方便讲解,提高论文的质量,另外一方面也是为了以后写论文打下良好的基础。 * 这篇文章会根据我以前的画图经验展开,以前是使用绘图软件,手动绘图。后来,看到了其他的方法,但是需要写一些代码,或者需要安装特定的软件,投入成本太大,就没有学。所以,本文分享的工

Blackoutdragon的博客 1万+

uniapp微信小程序 - 解决输入框input软键盘弹起时页面整体上移问题,页面上推导致顶部出现元素遮挡问题,uniapp软键盘导致页面上移头部内容被错位遮住错乱,让页面头固定在顶部不动不会受影响!

解决uni-app微信小程序底部input输入框,键盘弹起时页面整体上移问题,当键盘弹起时,会导致页面整体上移,页面头固定在顶部不动,微信小程序键盘弹起时,底部输入框抬起至键盘上方,顶部导航栏固定不动,uni-app输入框,输入框在底部;当弹出键盘时,页面整体上移,页面头信息会消失不见,小程序底部input输入框,input组件弹起系统键盘时,键盘会顶住页面导致页面会上移,当键盘弹起时整个页面被挤压,导致元素上移或被覆盖,输入框能自动抬起至键盘上方,顶部导航栏保持固定,不被顶上去,如何解决这个问题,动态高度

✅ 精选推荐文章 1万+

LeetCode 力扣 111. 二叉树的最小深度 minimum depth of binary tree

大家觉得写还可以,可以点赞、收藏、关注一下吧! 也可以到我的个人博客参观一下,估计近几年都会一直更新!和我做个朋友吧!https://motongxue.cn 文章目录111. 二叉树的最小深度题目描述示例:分析代码提交结果 111. 二叉树的最小深度 题目描述 给定一个二叉树,找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 说明: 叶子节点是指没有子节点的节点。 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20

motongxue的博客 248

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

LeetCode 111. Minimum Depth of Binary Tree 题解

本题是二叉树的经典问题,主要考察对树的遍历和递归的理解。递归深度优先搜索:代码简洁易懂,逻辑清晰,但在树深度较大时可能会遇到栈溢出的问题。迭代广度优先搜索:在遇到第一个叶子节点时立即返回,不需要遍历整个树,对于某些情况更高效。在实际应用中,广度优先搜索通常是解决最小深度问题的首选方法,因为它可以在找到第一个叶子节点时立即返回,避免了不必要的计算。

cannonjinx的博客 4463

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Leetcode 111 Minimum Depth of Binary Tree(水)

题目连接:Leetcode 111 Minimum Depth of Binary Tree解题思路:递归,每次返回子树中层数低的一个加一。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * ...

不慌不忙、不急不躁 208

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

【LeetCode】111. Minimum Depth of Binary Tree

两种解法: (1)深度优先遍历,与maximum depth of binary tree类似,时间复杂度,递归函数栈结构空间复杂度,但需要考虑两种情况: i. 节点返回时必须为叶子节点,返回深度应该为1。 ii. 为了满足i.实现时要返回非空子树的最小深度。 (2)广度优先遍历,优势在于,深度优先遍历会遍历全部子树,这在树非常不平衡时效率比较低。可以采用广度优先遍历来克服...

weixin_42344158的博客 257

Leetcode【111】Minimum Depth of Binary Tree(Python版)

# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def minDepth(sel...

ssjdoudou的博客 7056

LeetCode之111. Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note:A leaf is a node with no childre...

iNiBuBian的博客 429

[leetcode 111] Minimum Depth of Binary Tree

题目要求: Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest

iam_lain的博客 461

leetcode 111. Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note: A leaf is a ...

Weixu Liu的博客 183

Leetcode 111. Minimum Depth of Binary Tree

Leetcode 111. Minimum Depth of Binary Tree 题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note:A leaf is a node with no children. ..

sammydai的博客 178

闯关leetcode——111. Minimum Depth of Binary Tree

leetcode第111题

方亮的专栏 891

leetcode[111]Minimum Depth of Binary Tree

问题:给定二叉树,找其最小深度(叶子节点到根节点最短路径上的节点个数) 输入:TreeNode* root 输出:minimum_depth 思路:递归。一开始想着,只要一个儿子结点为NULL,便得到了最小深度,其实不是的,输入样例[1,2]便可证明。 看了[1]才明白自己错在哪里了,题目要求的是叶子结点到根结点最短路径,必须找到叶子结点,才可退出递归。 /** * Definiti...

Black Mamba的博客 170

【LeetCode111】Minimum Depth of Binary Tree

Runtime:8 ms, faster than95.80%ofC++online submissions forMinimum Depth of Binary Tree. Memory Usage:19.5 MB, less than90.48%ofC++online submissions forMinimum Depth of Binary Tree. 实...

weixin_39458342的博客 163

LeetCode111—Minimum Depth of Binary Tree

LeetCode111—Minimum Depth of Binary Tree

上善若水 393

leetcode笔记 111. Minimum Depth of Binary Tree & 112. Path Sum

leetcode刷题16天 111. Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest lea...

qq_29868413的博客 148
上一篇: [LeetCode] 104. Maximum Depth of Binary Tree
下一篇: [LeetCode] 110. Balanced Binary Tree
Ber03
博客等级 码龄9年 7粉丝 169原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值