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

Example:

Given binary tree [3,9,20,null,null,15,7],

    3
/ \
9 20
/ \
15 7

return its minimum depth = 2.

 public int minDepth(TreeNode root) {//树 递归 mytip
if(null==root){
return 0;
}
int left = minDepth(root.left);
int right = minDepth(root.right);
int d=left>right?(right+1):(left+1);
if(left==0||right==0){//注意 单孩子的判断
d= left+right+1;
}
return d;
}

还可以使用BFS的方法

相关题

二叉树的最大深度 LeetCode104 https://www.cnblogs.com/zhacai/p/10429258.html

LeetCode-111.Mininum Depth of Binary Tree的更多相关文章

  1. [leetcode] 111.Mininum Depth of Binary Tree (Easy)

    原题 寻找二叉树最短深度 这里用了dfs,beat 100%,4ms class Solution { public: int minDepth(TreeNode *root, int minNum ...

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

  3. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  4. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

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

  6. leetcode 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

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

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

  9. (二叉树 BFS DFS) 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 ...

  10. Java for 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 ...

随机推荐

  1. Ant与Proguard集中

    示例: <taskdef resource="proguard/ant/task.properties"         classpath="/usr/local ...

  2. Android APK代码混淆与资源混淆详解,你确定不看?

    APK的混淆分为资源混淆与代码混淆.一般大部分都使用两者结合.尤其是目前主流的应用. 其中的优点: 防止被恶意破解逆向分析 减少apk体积,也是瘦身的方法 代码可阅读性降低 其中的缺点: 调试不方便( ...

  3. Android异步处理系列文章四篇之二 使用AsyncTask异步更新UI界面

    Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面Android异步处理二:使用AsyncTask异步更新UI界面Android异步处理三:Handler+Loope ...

  4. [React] 14 - Redux: Redux Saga

    Ref: Build Real App with React #14: Redux Saga Ref: 聊一聊 redux 异步流之 redux-saga  [入门] Ref: 从redux-thun ...

  5. bootstrap入门基础

    1.字体 text-left text-center text-right text-lowercase 小写 text-uppercase 大写 text-capitalize 首字母大写 2.表格 ...

  6. [Ubuntu] LightDM 轻量级桌面显示管理器

    LightDM(Light Display Manager)是一个全新的轻量级 Linux 桌面显示管理器,而传统的 Ubuntu 是使用 GNOME 桌面标准的 GDM. LightDM 是一个跨桌 ...

  7. [转]Git忽略规则及.gitignore规则不生效的解决办法

    在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如无,则需自己手工建立此文件).这个文件每一行保存了一个匹配的规则例如: # 此为注 ...

  8. [原]jenkins(六)---jenkins远程部署脚本

    /** * lihaibo * 文章内容都是根据自己工作情况实践得出. * 版权声明:本博客欢迎转发,但请保留原作者信息! http://www.cnblogs.com/horizonli/p/533 ...

  9. [JavaScript] 配置JavaScript BUILD

    <project name="eForm" default="concatenate"> <tstamp> <format pro ...

  10. webpack构建工具快速上手指南

    最近在研究react项目,接触到webpack打包工具.刚接触的时候一脸茫然,经过最近的学习,下面我来带大家开启webpack入门之旅. webpack是什么 webpack是近期最火的一款模块加载器 ...