Java for LeetCode 111 Minimum Depth of Binary Tree
2024-09-08 03:45:50
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.
解题思路:注意minimum depth最后遍历的那个点,left right都必须为null,JAVA实现如下:
public int minDepth(TreeNode root) {
if(root==null)
return 0;
else if(root.left==null)
return minDepth(root.right)+1;
else if(root.right==null)
return minDepth(root.left)+1;
else return Math.min(minDepth(root.left),minDepth(root.right))+1;
}
最新文章
- 利用WCF创建简单的RESTFul Service
- 用Chrome插件对自动化测试TestWriter进行录制
- 【CCL】连通区域提取
- IOS- 单例
- Git使用ssh key
- AES加密算法-128位高安全,高速度
- 使用cwRsync实现windows下文件定时同步【转】
- SharePoint ListTemplateType enumeration
- 【最短路】BAPC2014 B Button Bashing (Codeforces GYM 100526)
- Python 变量有效范围
- Almost Prime
- docker异常问题解决
- java 字节码 指令集 汇编(转)
- [Android Security] APK自我保护 - DEX/APK校验
- C++中前置声明的应用与陷阱
- 【React】使用 create-react-app 快速构建 React 开发环境
- [CF1105D]Kilani and the Game
- Kerberos的白银票据详解
- java 中的try catch在文件相关操作的使用
- ActiveMQ简述
热门文章
- ios文件管理
- Struts2学习记录-Value Stack(值栈)和OGNL表达式
- mysql if()一个奇怪的问题
- 【秀优越(xie e)】原来出题也能够这么恶心。
- Linux 的计划任务(运维基础|可用于提权)
- vs 已经加入了引用,编译还是提示没有加入引用
- 【Excle数据透视表】如何快速选取所有标签并标注黄色底纹
- SQLAlchemy使用笔记--SQLAlchemy ORM(三)
- rtems 4.11 console驱动 (arm, beagle)
- java 表示当前时间的第二天的几点