题目:

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.

思路:

  • 题意是求一颗二叉树的的最短路径
  • 思路可以参考求二叉树的深度的算法,还是用递归的思想,考虑上级节点和下级左右子树的关系

代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int minDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        if(root.left != null && root.right == null){
            return 1+minDepth(root.left);
        }else if(root.left == null && root.right != null){
            return 1+minDepth(root.right);
        }else{
            return 1+Math.min(minDepth(root.left),minDepth(root.right));
        }
    }
}

LeetCode(37)-Minimum Depth of Binary Tree的更多相关文章

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

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

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

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

  3. LeetCode OJ Minimum Depth of Binary Tree 递归求解

        题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...

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

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

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

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

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

  8. leetcode 111 minimum depth of binary tree

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

  9. 【leetcode】Minimum Depth of Binary Tree

    题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

随机推荐

  1. 06 Activity隐式跳转

    一,隐式跳转 某个Activity 需要在清单文件配置某个Activity如下信息: 注意:category要和action一起用 action:制定一个活动 在隐式跳转的可以用到 category ...

  2. Maven2插件开发入门

    一.创建Maven项目 首先创建一个Maven插件项目,可以手动或使用mvn archetype:create从原型创建.pom.xml配置如下: 1 2 3 4 5 6 7 8 9 10 11 12 ...

  3. 学习TensorFlow,多层卷积神经网络

    一.网络结构 二.代码 from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_ ...

  4. 最简单的基于FFmpeg的视频编码器-更新版(YUV编码为HEVC(H.265))

    ===================================================== 最简单的基于FFmpeg的视频编码器文章列表: 最简单的基于FFMPEG的视频编码器(YUV ...

  5. JVM的内存区域模型

    首先要明白一个概念,就是JVM的内存区域划分与java的内存区域模型是两个不同的概念,前者指的是在java中jvm会将一个程序划分为哪些块来存储对应的数据,后者是一个更宏观上的j概念,指的是java线 ...

  6. 最简单的基于FFmpeg的AVfilter例子(水印叠加)

    ===================================================== 最简单的基于FFmpeg的AVfilter例子系列文章: 最简单的基于FFmpeg的AVfi ...

  7. Tom DeMarco:软件工程这个概念已过时?

    原文作者:Tom Demarco,写于2009年7月 作者简介:Tom DeMarco是大西洋系统协会(www.atlsysguild.com)的负责人.他的职业生涯开始于贝尔实验室,是结构化分析和设 ...

  8. C#attribute-----------初级

    前言: attribute是 .net FrameWork 提出的技术,可以为自己的代码添加注解,从而实现些特殊功能. 一. attribute功能 attribute被译作特性,既然是特性,必然功能 ...

  9. 【一天一道LeetCode】#93. Restore IP Addresses

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  10. UNIX环境高级编程——UNIX基础知识

    1.用户在登陆linux系统时,先键入登录名,然后键入口令.系统在其口令文件(通常是/etc/passwd文件)中查看登录名.口令文件中的登陆项由7个以冒号分隔的字段组成,它们是:登录名.加密口令.数 ...