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. [原创] Xinput_1.3.DLL / MSVCR100.DLL文件缺失解决办法

    缺少如上文件,最简单粗暴的办法就是360卫士,找人工服务搜索方案,一键修复,屡试不爽!

  2. circRNA 在人和小鼠脑组织中的表达

    circRNA 是一类动物体内的内源性的RNA,尽管circRNA的种类丰富,但是其在神经系统中的 功能,并不清楚.科学家通过对人和小鼠的不同脑部组织的RNA 测序,发现了上千种circRNA,经过分 ...

  3. [UI] 05 - Bootstrap: built-in components

    前言 一.资源 From: http://www.imooc.com/code/3777 内置组件 一.缩略图 <div class="col-xs-6 col-md-3"& ...

  4. Linux下跑程序,防止命令终端(断网,断电)

    有时候我们在服务器上跑程序,会发现当我们离开窗口时,正在执行的程序中断了,这让人非常郁闷.下面介绍防止程序中断的方法: 新建一个名为yourname的Screen窗口: screen -S yourn ...

  5. C#自定义Button按钮控件

    C#自定义Button按钮控件 在实际项目开发中经常可以遇到.net自带控件并不一定可以满足需要,因此需要自定义开发一些新的控件,自定义控件的办法也有多种,可以自己绘制线条颜色图形等进行重绘,也可以采 ...

  6. 浅谈MVC、MVP、MVVM

    MVC M: Model            模型——数据            (对于前台而言例如:ajax.jsonp等从后台获取数据的) V:  View             视图——表现 ...

  7. day_5.27python网络编程

    开始进行python网络编程2018-5-27 20:27:30 Tcp/Ip协议

  8. B - Broken Keyboard (a.k.a. Beiju Text) 数组模拟链表

    You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...

  9. linux 下修改键盘映射

    参考文档 原因: 输入 键盘原本的 “\ |”,结果映射到 "< >",而 < 与 > 对应的键名分别是 less 与 greater 查看键名: xev ...

  10. ERP项目实施记录08

    已经和软件商签订报价软件合同过去了2周,五一过后会有测试版本出来. 在整个接触过程中感觉开发方思路就是照葫芦画瓢,拿着这边的表单尽量往软件里搬,没有理清思路又急着赶进度:另外又要兼顾他们现有的&quo ...