/*
最小深度就是根节点到最近叶子节点的路径长度
和找二叉树的最大深度类似,但是区别如下:
递归终止条件:
1.根节点为null时,返回0
2.根节点左右都null时,返回1
3.大返回终止条件:返回较小值+1
但是需要注意的是:
这样就无法保证root是否是叶子节点了,
*/
public class Solution {
public int minDepth(TreeNode root) {
if(root==null) return 0;
if(root.left==null) return minDepth(root.right)+1;
if(root.right==null) return minDepth(root.left)+1;
return Math.min(minDepth(root.right), minDepth(root.left)) + 1;
}
}
public class Solution {
public int minDepth(TreeNode root) {
if(root == null) {
return 0;
}
if(root.left == null && root.right == null) {
return 1;
}
int l,r;
if(root.left != null) {
l = minDepth(root.left);
} else {
l = Integer.MAX_VALUE;
}
if(root.right != null) {
r = minDepth(root.right);
} else {
r = Integer.MAX_VALUE;
}
return l>=r?r+1:l+1;
}
}
相关推荐
C#集成Bartender:动态图片标签打印的实战与优化
本文详细介绍了在C#程序中集成Bartender实现动态图片标签打印的实战方法与优化策略。通过具名数据源绑定、图片对象尺寸控制等核心技巧,解决了动态图片显示/隐藏的难题,并提供了从模板设计、代码架构到性能优化、错误处理的全流程指南,帮助开发者构建稳定高效的工业级标签打印系统。
LeetCode-111. 二叉树的最小深度(java)
金三银四,又到了刷题月。有时间的话,希望大家还是尽量刷起来咯。帮助往往不是直接体现出来的,但会让你对很多东西的底层实现了解的更深刻。
使用GitLab+Drone CI持续集成自动部署web项目,图文详细
kind: pipeline # 定义对象类型,还有secret和signature两种类型 type: docker # 定义流水线类型,还有kubernetes、exec、ssh等类型 name: mall-tiny-drone # 定义流水线名称 steps: # 定义流水线执行步骤,这些步骤将顺序执行 - name: package # 流水线名称 image: maven:3-jdk-8 # 定义创建容器的Docker镜像 volumes: # 将容器内目录挂载到宿主机,仓库
数据结构之树--111. 二叉树的最小深度
给定一个二叉树,找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 说明:叶子节点是指没有子节点的节点。 示例 1: 输入:root = [3,9,20,null,null,15,7] 输出:2 /** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left
国土变更调查中六类拓扑错误的成因分析与高效修复实践
本文深入分析了国土变更调查中六类拓扑错误的成因,包括边界不一致和不规则图形等常见问题,并提出了高效的自动化修复实践。通过智能咬合算法、引力-斥力平衡模型等核心技术,显著提升了修复效率,节省人工质检时间70%。文章还分享了实战中的避坑指南和黄金法则,为相关从业人员提供了宝贵经验。
uniapp(全端兼容) - 最新详细实现接入腾讯视频播放器功能插件,uniapp全平台端(小程序/H5网页/App/nvu)调用引入并使用腾讯视频播放组件完整全流程(详细示例源码,一键复制开箱即用)
uniapp腾讯视频,uniapp微信小程序腾讯视频,uniapp小程序接入腾讯视频详细示例教程源码,uniapp怎么在微信小程序平台使用腾讯视频插件,在uni-app中使用腾讯视频插件播放视频,uniapp调用腾讯视频,uniapp引入腾讯视频插件,uniapp小程序中引入腾讯视频无法播放,uniapp微信小程序平台引入腾讯视频播放不了黑屏,在uniapp开发的微信小程序中使用腾讯视频,uniapp如何引入腾讯视频插件播放视频,uni-app小程序平台引入腾讯视频插件播放腾讯视频,uniapp 小程序接入
LeetCode 111.二叉树的最小深度(Java)
先定义一个整数len用来表示队列的长度que.size(),当len-->0时,mindepth++,然后从队列中取出一个节点,如果左右节点同时为空,则找到了最小深度并返回,否则判断左右子节点是否为空,再把节点的左右子节点加入到que中。判断一个二叉树的最小深度是通过层序遍历找到第一个左右节点都为空的节点。层序遍历法除了要返回的list之外还要借助queue这个数据结构。当节点不为空时,将其加入到队列中,当队列不为空的时候,开始遍历。如果根节点为空,则直接返回。本题可采用层序遍历法。
实现画布拖拽算子排布,通过flink实现算子编排计算,如何实现?
🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。
LeetCode第 111 题:二叉树的最小深度(C++)
111. 二叉树的最小深度 - 力扣(LeetCode) 注意叶子节点的定义。 因为求的是最小深度,所以可以使用层次遍历(bfs): class Solution { public: int minDepth(TreeNode* root) { if(!root) return 0; int res = 0; queue<pair<TreeNode*, int>> q; q.push({root, 1});
备赛电赛学习硬件篇(二):电源板电路设计
1、输入接口要准备各种接口,如2p端子,锂电池接口,typec等,提供多种电源供电方案2、输出接口3、自由拓展。
基于FPGA的64点fft变换verilog开发
这精度的损失在10bits看来对最后的误差的影响是存在是不可忽略的。此后,在这思想基础上又开发了高基和分裂基等快速算法,随着数字技术的高速发展,1976年出现建立在数论和多项式理论基础上的维诺格勒傅里叶变换算法(WFTA)和素因子傅里叶变换算法。采用这种算法能使计算机计算离散傅里叶变换所需要的乘法次数大为减少,特别是被变换的抽样点数N越多,FFT算法计算量的节省就越显著。采用这种算法能使计算机计算离散傅里叶变换所需要的乘法次数大为减少,特别是被变换的抽样点数N越多,FFT算法计算量的节省就越显著。
Java实现LeetCode 111. Minimum Depth of Binary Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { publ...
精雕软件(JDpaint) 5.5 绿色版
精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版
【Java】【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 ch...
jre9下载 jdk9
(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】
VB摄像头监控
该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制
LeetCode 111. Minimum Depth of Binary Tree【Java】
题目描述 111. 二叉树的最小深度 AC代码 本题让找最小深度,题目对最小深度的定义是:从根节点到最近叶子节点的最短路径上的节点数量。其实可以理解为求左右子树的深度,然后取较小的那一个作为答案。 注意,如果一棵树只有根节点,那么答案应该是1,为什么呢?题目中对叶子节点的定义是指没有子节点的节点。 再看其他情况: 当 root 节点左右孩子有一个为空时,返回不为空的孩子节点的深度,在这里我们可以把...
Java [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. 解题思路: 递归。 代码描述: /** ...
【leetcode】111. Minimum Depth of Binary Tree【java】
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. /** * Definition for a bin
[LeetCode] 111. Minimum Depth of Binary Tree Java
题目: 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. 题意及分析:求一棵二叉树的最低高度,即根节点到叶子节点的...
LeetCode 111. Minimum Depth of Binary Tree java
题目描述: 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 ...
Java描述 LeetCode,111. Minimum Depth of Binary Tree 二叉树的最小深度
大家好,我是河海哥,专注于后端,如果可以的话,想做一名code designer而不是普通的coder,一起见证河海哥的成长,您的评论与赞是我的最大动力,如有错误还请不吝赐教,万分感谢。一起支持原创吧!纯手打有笔误还望谅解。 1-1:description Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root n.
LeetCode 111. Minimum Depth of Binary Tree 题解——Java
题目链接:https://leetcode.com/problems/minimum-depth-of-binary-tree/#/description 思路:二叉树的最小深度 = min {左子树的最小深度,右子树的做小深度 } + 1 ;使用递归求解。 需要注意的是,求二叉树的最小深度比求二叉树的最大深度稍微复杂一点,有一种情况需要考虑:当根节点只有左子树或者只有右子树时...假设某二
LeetCode 111.Minimum Depth of Binary Tree/ c++ & java & JavaScript
问题描述: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. 找二叉树的最小深度 java版本答案: /** * Defin...
【LeetCode】 111. Minimum Depth of Binary Tree 二叉树的最小深度(Easy)(JAVA)
【LeetCode】 111. Minimum Depth of Binary Tree 二叉树的最小深度(Easy)(JAVA) 题目地址: https://leetcode.com/problems/minimum-depth-of-binary-tree/ 题目描述: Given a binary tree, find its minimum depth. The minimum depth...
Leetcode111.二叉树的最小深度 Minimum Depth of Binary Tree(Java)
Leetcode111.二叉树的最小深度 Minimum Depth of Binary Tree(Java) ##Tree##, ##Depth-first Search##, ##Breadth-first Search## 二叉树的最小深度,可以采用DFS、BFS DFS对应二叉树的后序遍历,递归计算每个节点的最小深度 结点为空时,返回0 结点的左儿子不为空,右儿子为空,返回左子树最小深度 + 1 结点的右儿子不为空,左儿子为空,返回右子树最小深度 + 1 结点的左右儿子都不为空,返回左右子树最小深
431



被折叠的 条评论
为什么被折叠?



