155. Minimum Depth of Binary Tree【LintCode by java】
Description
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.
Example
Given a binary tree as follow:
1
/ \
2 3
/ \
4 5
The minimum depth is 2
.
题意:求二叉树的最小深度。至于这个最小深度该怎么理解呢?接触的比较多的是最大深度,即根结点到离它最远的叶子结点的距离(包括首尾),这里的距离当然是指结点的个数。显然,最小深度就是根结点到离它最近的结点之间距离。虽然知道是这个意思,在一开始的理解上还有点偏差。例如,我想如果根结点只有右子树,那么这个最小深度怎么算,是不是就是1了,因为左子树为空嘛。后来仔细想想,其实不然,左子树为空的话,如果右子树不为空,说明根结点(其他结点亦然)不是叶子点,还得继续求右子树的深度(对应于代码的第26和29行),直到遇到叶子结点。代码如下:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
* @param root: The root of binary tree
* @return: An integer
*/
public int minDepth(TreeNode root) {
// write your code here
if(root==null){
return 0;
}
if(root.left==null&&root.right==null){
return 1;
}
if(root.left==null){
return minDepth(root.right)+1;
}
if(root.right==null){
return minDepth(root.left)+1;
}
return Math.min(minDepth(root.left),minDepth(root.right))+1;
}
}
155. Minimum Depth of Binary Tree【LintCode by java】的更多相关文章
- 175. Invert Binary Tree【LintCode by java】
Description Invert a binary tree. Example 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 解题:题目要求讲二叉树的左子树和右子树对调 ...
- leetcode:Minimum Depth of Binary Tree【Python版】
1.类中递归调用添加self: 2.root为None,返回0 3.root不为None,root左右孩子为None,返回1 4.返回l和r最小深度,l和r初始为极大值: # Definition f ...
- 【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练习题】Minimum 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 (2 solutions)
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- [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 ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- LeetCode My Solution: Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...
随机推荐
- Linux NFS 详解
目录: 1.了解NFS服务 2.NFS主要文件 3.NFS安装 4.NFS配置 5.NFS客户端配置 6.固定NFS端口 7.Windows挂 ...
- 6 Dockerfile指令详解 && ENTRYPOINT 指令
ENTRYPOINT 的格式和 RUN 指令格式一样,分为 exec 格式和 shell 格式. ENTRYPOINT 的目的和 CMD 一样,都是在指定容器启动程序及参数. ENTRYPOINT 在 ...
- Python2.7 - IMOOC - 1
第一章 课程介绍 python 特点:优雅.明确.简单 python适合的领域:1.Web网站和各种网络服务:2.系统工具和脚本:3.作为"胶水"语言把其他语言开发的模块包装起来方 ...
- 有关js弹出提示框几种方法
1直接提示只有确定功能的提示框 只显示提示信息 alert(“提示信息”); alert ();的参数只有一个就是提示信息,无返回值 2 弹出输入框让你输入内容 prompt() ; 有两个参数:第一 ...
- BZOJ 2038 小Z的袜子(hose) 莫队算法模板题
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2038 题目大意: 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中 ...
- cocos2d::CCFileUtils::sharedFileUtils()->getFileData(szFile, "r", &bufferSize) 不同平台返回值不一样
string pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName); unsigned char* pB ...
- node.js的npm命令常见错误及解决方案
使用npm命令进行模块安装的时候场出现各种错误,本文总结我所遇到的各种错误,并提供解决方案.(大部分内容为网上收集) 首先使用淘宝 NPM 镜像 大家都知道国内直接使用 npm 的官方镜像是非常慢的, ...
- Kali-linux无线网络嗅探工具Kismet
如果要进行无线网络渗透测试,则必须先扫描所有有效的无线接入点.刚好在Kali Linux中,提供了一款嗅探无线网络工具Kismet.使用该工具可以测量周围的无线信号,并查看所有可用的无线接入点.本节将 ...
- selenium + python自动化测试unittest框架学习(五)webdriver的二次封装
因为webdriver的api方法很长,再加上大多数的定位方式是以xpath方式定位,更加让代码看起来超级长,为了使整体的代码看起来整洁,对webdriver进行封装,学习资料来源于虫师的<se ...
- jquery mobile各类标签的refresh
JQM里面当我们更新了某些页面标签(如: listview, radiobuttons, checkboxes, select menus)里的数据时,必须做refresh操作. 为什么必须做refr ...