111. Minimum Depth of Binary Tree

深入解析EXI协议在ISO15118-2命令测试中的编解码实践 本文深入探讨了EXI协议在ISO15118-2车桩通信标准中的关键作用与实践方法。EXI编解码测试是确保通信准确性与效率的核心环节,文章详细介绍了从搭建测试环境、选择工具库到编写完整测试代码的全过程,并分析了结果差异的常见原因与调试技巧,为电动汽车充电系统的开发者提供了实用的技术指南。 阅读详情
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.

解法一:BFS

class Solution {
public:
    int minDepth(TreeNode* root) {
        int count = 0;
        if(root == NULL)
            return count;

        queue<TreeNode*> que;
        que.push(root);
        while(!que.empty()){
            ++count;
            const int size = que.size();
            for(int i=0; i<size; ++i){
                TreeNode* tmp = que.front();
                que.pop();

                if(tmp->left == NULL && tmp->right == NULL)
                    goto final;

                if(tmp->left != NULL)
                    que.push(tmp->left);
                if(tmp->right != NULL)
                    que.push(tmp->right);
            }
        }
final:
        return count;
    }
};

注意双层循环,仅仅使用break是无法跳出来的,使用goto,或者直接return。

解法二:DFS

  if(root == NULL)
            return 0;
        int l = minDepth(root->left);
        int r = minDepth(root->right);
        return 1 + ((l != 0 && r != 0) ? std::min(l, r) : std::max(l, r));

注意当递归回去的时候,某个节点当前深度为0,我们要return较大的哪一个,否则此处节点的深度就成了“假0”!

别再死记硬背了!用‘小乌龟’实战图解ROS1核心指令(rosnode/rostopic/rosparam保姆级手册) 本文通过经典的turtlesim乌龟仿真器,以可视化实战方式讲解ROS1核心指令(rosnode/rostopic/rosparam),帮助初学者摆脱死记硬背,在操控乌龟的过程中轻松掌握节点管理、话题通信和参数设置等ROS1核心概念。 阅读详情

相关推荐

如何利用GMS构建地下水数值模拟模型?

第一步:点图中三维网格的图标然后点Grid→Create Grid这个界面是用于创建一个有限差分网格的设置窗口。它允许你定义网格在三维空间中的范围、分辨率以及其他相关属性。这个界面提供了一个直观的方式来定义一个三维的有限差分网格,你需要指定每个维度上的范围、划分的单元格数量,以及一些控制单元格大小分布和网格方向的参数。这个网格通常会被用于数值模拟,例如地下水流模拟。在点击 "OK" 之前,请务必仔细检查所有参数是否符合你的建模需求。1. X-Dimension (X 轴维度)2. Y-Dimension (

weixin_44259522的博客 2097

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.

老夫编代码就是一把梭 594

【AI生成】市面上主流自组网厂家的技术及市场研究

经过对全球自组网产业技术特征与市场格局的系统分析,本文得出以下核心结论:技术演进呈现智能化与融合化双轨路径 当前自组网技术创新已突破传统组网架构局限,形成"协议优化-架构创新-场景适配"的递进式发展路径。国际厂商聚焦智能算法应用,如Silvus采用深度学习实现动态拓扑优化,传输时延较传统方案降低37%;国内企业则侧重异构网络融合,中国移动设计院提出的5G LAN架构使工业场景通信可靠性提升至99.99%。

qq_31310977的博客 1699

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的专栏 687

FPGA防抄板实战:如何利用Xilinx芯片的“身份证”(DNA)实现你的加密锁?

本文深入探讨了如何利用Xilinx FPGA芯片的DNA(Device Native Authentication)功能实现硬件级加密保护,防止设计被抄袭。文章详细解析了DNA的物理实现、获取方式,并提供了从DNA到加密密钥的转换方法,以及构建商业级加密防护系统的实战方案。通过JTAG和原语两种读取方式的对比,帮助开发者选择最适合的方案。

weixin_42548829的博客 385

如何实现VMware虚拟机上的ubuntu与windows主机共享文件夹

windows主机部分   在硬盘上任意位置新建文件夹,此处命名为Share,实际上随意。 接下来右键属性,操作如下系列图所示: 在硬盘上任意位置新建文件夹,此处命名为Share,实际上随意。 成功修改后,显示如下界面: VMware部分 ubuntu在关机状态下,点击“编辑虚拟机设置” 接下来,开启ubuntu系统,在终端输入: ·· vmware-hgfsclient ·· 将会显示如下画面,但是此时你可能找不到 /mnt/hgfs 这个路径,如果找不到该路径,则需要在虚拟

Mr.Sliver的博客 8221

[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

劳特巴赫 Trace32 调试使用教程

详细介绍如何使用trace32进行调试

m0_56208280的博客 4万+

9.14 DFS 简单 111 Minimum Depth of Binary Tree 112 Path Sum

111 DFS 不太熟练的用法 好法多磨 112 DFS 比较典型的例子

emmm_Yeah的博客 292

Matlab激活失败?手把手教你解决License checkout failed问题(附详细截图)

本文针对Matlab激活后出现“License checkout failed”错误的常见问题,提供了详细的解决方案。核心在于精准定位并正确填写Windows系统用户名,而非计算机名或账户全名。文章通过分步截图和命令指引,手把手教你使用`whoami`命令获取正确信息,并重新运行激活程序,从根本上解决因用户名填写错误导致的许可检查失败问题。

weixin_29062823的博客 585

程序化广告行业(2/89):从程序化广告深挖数据处理技巧

大家好!我一直希望能和大家在技术学习的道路上携手共进,这也是我写这一系列博客的初衷。上次我们一起学习了Python基础的数据处理知识,这次咱们借助一本程序化广告手册,深入挖掘数据处理的更多技巧。这本手册涵盖了程序化广告从概念到技术实现的诸多方面,而其中处处都涉及到数据处理,相信会给我们带来不少收获

yyy173611的博客 4572

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; * ...

不慌不忙、不急不躁 207

手把手教你学Simulink——基础储能管理场景实例:基于Simulink的压缩空气储能(CAES)系统简化模型仿真

本文介绍了基于Simulink的压缩空气储能(CAES)系统简化模型仿真方法。CAES作为大规模长时储能方案,相比锂电池具有成本低、寿命长等优势。文章详细讲解了AA-CAES系统组成、热力学建模理论,以及Simulink实现流程,包括储气室、压缩机、膨胀机等核心模块的建模方法。通过8小时充电(50MW)和6小时放电(40MW)的仿真,验证了系统可实现60%往返效率。该模型为理解CAES系统运行机制提供了实用工具,并指出热管理、储气室特性等工程实践要点,为新型电力系统储能规划提供了仿真基础。

MHD0815的博客 173

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

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

【LeetCode】111. Minimum Depth of Binary Tree

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

weixin_42344158的博客 257

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

111. Minimum Depth of Binary Tree [easy] (Python)

题目链接https://leetcode.com/problems/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 no

Coder_Orz的博客 3898

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

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. 即求根结点到其叶节点的最短距离。 简单地用BFS就可以解决问

Lau_r_k的博客 361

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.分析求二叉树的最小深度:根节点到最近叶子节点的路径长度。同样采用递归的思想: 当根

指尖飞舞 2325

[leetcode] 111. Minimum Depth of Binary Tree 解题报告

题目链接:https://leetcode.com/problems/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 nod

小榕流光的专栏 1166

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的博客 7055

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 leaf node.

leo1949asd的专栏 684

【Leetcode】111. Minimum Depth of Binary Tree 解题报告

求树的最小深度,深度为根节点到叶子节点的长度 方法1 递归 对于某个非None节点我们返回其左右子树最小的深度,但是要注意返回的值是否为0,如果为0,其最小深度就是另外一个子树的深度 class Solution: def minDepth(self, root): """ :type root: TreeNode :rtype: int ...

很吵请安静 393
上一篇: 208. Implement Trie (Prefix Tree)--Trie树
下一篇: 55. Jump Game(贪心)
FreeeLinux
博客等级 码龄11年 421粉丝 356原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值