LeetCode OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/
Submissions: 312802 Difficulty: Easy
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.
Subscribe to see which companies asked this question
求一棵二叉树。离根近期的叶子的高度。递归找出两棵子树的最小高度加一便可求解。
我的AC代码
public class MinimumDepthofBinaryTree {
public int minDepth(TreeNode root) {
if(root == null) return 0;
return height(root);
}
private int height(TreeNode root){
if(root.left == null && root.right == null) return 1;
else if(root.left == null) return height(root.right) + 1;
else if(root.right == null) return height(root.left) + 1;
return Math.min(height(root.left), height(root.right)) + 1;
}
}LeetCode OJ Minimum Depth of Binary Tree 递归求解的更多相关文章
- LeetCode OJ——Minimum Depth of Binary Tree
http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ 贡献了一次runtime error,因为如果输入为{}即空的时候,出现了c ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- [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 shor ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- 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 th ...
- 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 shor ...
- LeetCode(37)-Minimum Depth of Binary Tree
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
随机推荐
- [知识复习] C语言文件读写
文件打开 fopen() 返回FILE* 对象,如果打开失败返回NULL,错误代码存入errno中 FILE *fopen( const char * filename, const char * m ...
- JMX monitor weblogic 总结
https://blog.csdn.net/joy_91/article/details/42774839
- 四十五 常用内建模块 hashlib
Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制 ...
- ffmpeg测试程序
ffmpeg在编译安装后在源码目录运行make fate可以编译并运行测试程序.如果提示找不到ffmpeg的库用LD_LIBRARY_PATH指定一下库安装的目录.
- JDK Windows安装
进入至JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 点击下载后,会进入下载列表 点击下载后,就等 ...
- PTA L1-020 帅到没朋友 团体程序设计天梯赛-练习集
L1-020 帅到没朋友(20 分) 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友.本题就要求你找出那些帅到没有朋友的人. 输入格式: 输入第一行给出一个正整数N(≤),是已 ...
- WeGame导致WSL无法监听端口问题
Windows 10 系统自带Linux子系统(WSL),可以方便的使用WSL运行Linux程序和脚本.笔者在WSL上运行Redis时突然发现无法监听6379端口,尝试重新安装WSL无果. 后来重新安 ...
- 一个Bean属性拷贝的工具类
package com.fpi.spring.qaepb.cps.util; import java.beans.IntrospectionException; import java.beans.P ...
- JNDI Tomcat
1.JNDI的诞生及简介简介 1)服务器数据源配置的诞生 JDBC阶段: 一开始是使用JDBC来连接操作数据库的: 在Java开发中,使用JDBC操作数据库的四个步骤如下: ①加载数据库驱动程序(Cl ...
- DHCP拒绝服务攻击工具DHCPig
DHCP拒绝服务攻击工具DHCPig DHCP服务负责网络的IP分配服务.通过攻击该服务,可以导致网络内主机获取不到IP,而无法正常使用网络.Kali Linux提供一款专用工具DHCPig.该工 ...