leetcode111---Minimum Depth of Binary Tree

第67步 时间序列建模实战:ARIMA建模(Stata) 阅读详情

问题描述:
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.

方法一:DFS

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode* root) {
        if(!root) return 0;
        int left = minDepth(root->left);
        int right = minDepth(root->right);
        return (left==0 || right==0)?(left+right+1):min(left, right)+1;
    }
};

方法二:BFS

class Solution {
public:
    // BFS
    int minDepth(TreeNode *root) {
        int res = 0;
        queue<TreeNode *> myQueue;
        if (root)
            myQueue.push(root);
        while (!myQueue.empty()) {
            int l = myQueue.size();
            res++;
            for (int i = 0; i < l; i++) {
                TreeNode *tmp = myQueue.front();
                if (!(tmp->left) && !(tmp->right))
                    return res;
                if (tmp->left)
                    myQueue.push(tmp->left);
                if (tmp->right)
                    myQueue.push(tmp->right);
                myQueue.pop();
            }
        }
        return res;
    }
};
OV9734 DATASHEET,完整规格书 OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等 立即下载

相关推荐

activemq_CVE-2015-5254_漏洞复现_源码分析

ActiveMQ-CVE-2015-5254

qq_51550750的博客 945

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

格拉姆角场(GAF)实战:用Python实现时间序列图像化处理

本文深入解析了格拉姆角场(GAF)技术,这是一种将一维时间序列转化为二维图像特征的方法,以便利用卷积神经网络(CNN)等图像模型进行深度分析。文章通过Python实战代码,详细演示了GASF和GADF两种核心转换的实现过程,并探讨了其在时序数据分类任务中的应用,为故障预警、金融模式识别等场景提供了强大的图像驱动分析视角。

weixin_29079671的博客 283

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

eVTOL热管理:从电动车到飞行器的技术跃迁与工程挑战

热管理系统是保障各类电动设备安全、高效运行的核心技术,其基本原理是通过冷却、加热和温度控制,确保电池、电机等关键部件在最佳工况下工作。在新能源汽车领域,热管理技术已相对成熟,但面对eVTOL(电动垂直起降飞行器)这一新兴领域,其设计逻辑需从二维地面转向三维空域,应对环境温度剧烈变化、重量约束极端严苛等全新挑战。eVTOL的热管理不仅是简单的技术移植,更涉及对电池瞬时高功率散热、乘员舱快速温控及多热源耦合等复杂工程问题的系统性解决,直接关系到飞行安全与商业航程。本文聚焦于**环境温度**和**有效载荷**等关

weixin_42521276的博客 623

Vivado SDK中LWIP库版本冲突与RTL8211FDI兼容性实战解析

本文深入解析Vivado 2019.1 SDK中LWIP 1.0库与RTL8211FDI千兆PHY芯片的版本冲突问题,并提供实战解决方案。通过手动移植高版本LWIP库、整合第三方驱动及优化BSP配置,有效解决了以太网通信兼容性问题,确保千兆性能稳定运行。

ios99的博客 281

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

开源CMDB与资产管理平台对比报告

本文对比分析了9款主流开源CMDB平台,从集成能力、功能完整性和多语言支持三个维度进行评测。重点介绍了VeCMDB、蓝鲸CMDB、WeCMDB和OpsAny等国产平台的特色:VeCMDB以动态模型和自动发现见长;蓝鲸CMDB适合超大规模企业级应用;WeCMDB专注金融级高并发场景;OpsAny提供一体化运维解决方案。针对不同规模企业给出选型建议:中小团队推荐Snipe-IT等轻量工具;大型企业建议蓝鲸或WeCMDB;需国产化适配的可选VeCMDB或OpsAny。文章还特别指出iTop、GLPI等适合ITIL

huang0612123的博客 1094

[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

ConvNeXt 系列改进:二次创新 ConvNeXt:结合 RepVGG 结构重参数化,训练多分支、推理单路

2026年的视觉模型赛道呈现出一种有趣的“返璞归真”趋势——在Transformer狂飙数年之后,卷积网络正以全新的姿态回归。这其中,ConvNeXt无疑是纯卷积阵营中最耀眼的明星。从2022年Meta AI首次提出至今,ConvNeXt系列已经走过了近四年的演进之路,衍生出ConvNeXt V2、E-ConvNeXt等多个增强版本。ConvNeXt的7×7深度卷积带来了不错的感受野,却也埋下了推理效率的隐患。

ZXSXJ的博客 246

解答网友疑问:聊聊 SAP MRP 中追溯需求的技术实现

笔者到 SE37 里一查,果然有这个 Function Module,并且在里面设置断点之后,执行事务码 MD09 之后,设置在该 Function Module 内的断点也触发了,看来 ChatGPT 比较靠谱。因为红色区域的显示,是通过用户在 MD09 的 ALV 界面上双击触发的,因此我们直奔用户输入模块,PROCESS AFTER INPUT,在 check_fcode 的实现体内设置断点。通过这种追溯,企业可以了解需求和供应之间的关系,从而优化生产和采购计划,减少库存积压,提升供应链效率。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1044

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

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

motongxue的博客 248

Python 爬虫实战:去哪儿网机票价格爬取与出行比价分析

本文介绍了一个基于Python的机票价格监控系统,通过爬取去哪儿网数据实现多航线查询与智能比价。系统采用requests+BeautifulSoup+selenium技术栈,具备以下功能: 实时查询指定航线价格 历史价格追踪与分析 最低价推荐与价格预警 数据可视化展示 文章详细解析了去哪儿网的页面结构和反爬机制,提供了Selenium浏览器的完整配置方案,并实现了一个可稳定运行的机票采集爬虫。该系统能帮助用户掌握价格波动规律,选择最优购票时机,每年可节省可观的出行成本。

u014481728的博客 705

LeetCode 111. Minimum Depth of Binary Tree 题解

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

cannonjinx的博客 4463

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

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

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

【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

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(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 ...

Weixu Liu的博客 183

[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

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

LeetCode111—Minimum Depth of Binary Tree

LeetCode111—Minimum Depth of Binary Tree

上善若水 393

[LeetCode] 111. Minimum Depth of Binary Tree

原题链接: 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 no...

Ber03的博客 178

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
上一篇: 工厂三兄弟之抽象工厂模式(二):产品等级结构与产品族
下一篇: 工厂三兄弟之抽象工厂模式(三):抽象工厂模式概述
爱橙子的OK绷
博客等级 码龄15年 351粉丝 329原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值