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. maven 打包报错(增加调试信息)

    eclipse配置debug详细信息 如下图:

  2. [React] 15 - Redux: practice IM

    本篇属于私人笔记. client 引导部分 一.assets: 音频,图片,字体 ├── assets │ ├── audios │ ├── fonts │ └── images 二.main&quo ...

  3. 8 -- 深入使用Spring -- 6...3 使用@Transactional

    8.6.3 使用@Transactional Spring还允许将事务配置放在Java类中定义,这需要借助于@Transactional注解,该注解即可用于修饰Spring Bean类,也可用于修饰B ...

  4. offsetHeight,clientHeight,scrollHeight,offsetY等属性的理解

    el.offsetHeight = height + padding + border(滚动条是在边框内的,自然也包括在内) el.clientHeight = 可视化看到的高度 (就是content ...

  5. TSPL学习笔记(1)

    扩展语法(Syntactic extensions) 扩展语法就是通过核心语法或已经定义的扩展语法创建一种新的语法模式. Scheme核心语法模式包括: 顶层定义 常量 变量 过程应用 '(quote ...

  6. MySQL 出现You can't specify target table for update in FROM clause错误解决方法

    MySQL出现You can’t specify target table for update in FROM clause 这个错误的意思是不能在同一个sql语句中,先select同一个表的某些值 ...

  7. [破解] nasca drm file -ver1.00

    在使用nasca系统中的下载相应的文件时,默认下载会被Nasca加密,可以通过下面的方式进行免除加密. 首先我们需要了解没有加密系统的默认下载过程: 当下载文件33333333333.pdf时,由于文 ...

  8. 6.18_web服务器内容

    #coding:utf-8 ''' 2018-6-18 14:47:23 创建一个静态服务器访问指定页面 http://127.0.0.1:8000/ ''' import socket from m ...

  9. window.history.go(-1)返回且刷新页面 点击返回上一层

    windows窗口对象(历史)history.go(),history.back(),history.forward(). 因为windows对象引用不是必须的.所以windows.history.g ...

  10. 高效办公必不可少的5个Excel技巧

    1.输入“001.002…”的编号 想要快速给表格添加上“001.002…”这样的编号,你可以这样做: 选择所有单元格——右键点击[设置单元格格式]——点击[文本]——点击[确定]即可. 2.单元格内 ...