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】的更多相关文章

  1. 175. Invert Binary Tree【LintCode by java】

    Description Invert a binary tree. Example 1 1 / \ / \ 2 3 => 3 2 / \ 4 4    解题:题目要求讲二叉树的左子树和右子树对调 ...

  2. 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 ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【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 ...

  5. 【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 ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. LeetCode My Solution: Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...

随机推荐

  1. August 08th 2017 Week 32nd Tuesday

    The very essence of romance is uncertainty. 浪漫的精髓就在于它充满种种可能. Romance is the glamour that can turn th ...

  2. ZT extern "C"的用法解析

    extern "C"的用法解析 1.引言 C++语言的创建初衷是“a better C”,但是这并不意味着C++中类似C语言的全局变量和函数所采用的编译和连接方式与C语言完全相同. ...

  3. Spring Security 静态资源访问

    在搞 Spring Security 的时候遇到了一个小坑,就是静态资源加载的问题. 当我们继承了 WebSecurityConfigurerAdapter的时候,会去重写几个方法.去设定我们自己要过 ...

  4. Foj 2299 Prefix(AC自动机、DP)

    Foj 2299 Prefix 题意 给定串s.正整数n,问有多少长度为n的字符串t满足:s[0...i]是t的子串,s[0...i+1]不是. 题解 求有多少长度为n的字符串t满足:s[0...i] ...

  5. PhoneGap的通知

    一.通知类型列表 1.notification.alert         提示警告框 2.notification.confirm                  确认对话框 3.notifica ...

  6. 学习python第二天数据库day1

    day01: 关键字:desc 作用:查看表结构(字段名,数据类型&长度) 举例: desc python1808_laoguo; 追加数据到表中:(新增操作) 关键字:insert into ...

  7. 【nodejs】学习笔记

    学习链接:http://www.cnblogs.com/zhongweiv/p/nodejs_environment.html (一)简介及环境安装 Node.js是让Javascript脱离浏览器运 ...

  8. Kali-linux使用Wifite破解无线网络

    一些破解无线网络程序是使用Aircrack-ng工具集,并添加了一个图形界面或使用文本菜单的形式来破解无线网络.这使得用户使用它们更容易,而且不需要记住任何命令.本节将介绍使用命令行工具Wifite, ...

  9. WEB安全 php+mysql5注入防御(二)

    第四天: 一.新的注入函数: ascii() substring("string",n,m)                     n>=1 limit n,m      ...

  10. GIT和GitHub的使用总结

    目录 一.GIT初始化设置: 创建密钥 添加远程仓库 用户名邮箱配置 Clone远程仓库 创建本地仓库 代理设置 自定义操作 二.GIT基本操作 添加到暂存区区 提交 删除 版本回退 撤销修改 三.G ...