题目:

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. SSH架构实现在线支付功能

    在线支付是指卖方与卖方通过因特网上的电子商务网站进行交易时,银行为其提供网上资金结算服务的一种业务,她为企业和个人提供了一个安全.快捷.方便的电子商务应用环境和网上资金结算工具,在线支付不仅帮助企业实 ...

  2. 如何查看App provision profile文件中的钥匙链访问组名称

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们因为某些原因希望安全的在多个App中共享一些信息,我们可以 ...

  3. IDEA阅读Spark源码

    将spark编译成idea-sbt工程 tar -zxvf spark-1.1.0.tgz cd spark-1.1.0 sbt/sbt gen-idea 等待-- 成功后就能以SBT工程的形式导入i ...

  4. CentOS一般用户和root用户之间的切换

    如果终端提示符显示为"$",表明该用户为普通用户.输入su,回车,然后输入root密码,即可切换到root用户.如果是root用户想切换回普通用户,输入"su 用户名&q ...

  5. Android初级教程:shape的基本用法

    转载本文请注明出处:http://blog.csdn.net/qq_32059827/article/details/52203347   点击打开链接 在自定义进度条之前,先来学习一下shape的用 ...

  6. Android 9Patch图片的使用-android学习之旅(十八)

    9patch的使用方法 9patch图片常被用来做消息发送等的图片,只是缩放照片的部分区域,使得图片的整体形状不会受到影响,比较方便. 下面我们介绍一下: 在android的SDK安装目录下的tool ...

  7. UNIX网络编程——客户/服务器程序设计示范(二)

        TCP并发服务器程序,每个客户一个子进程 传统上并发服务器调用fork派生一个子进程来处理每个客户.这使得服务器能够同时为多个客户服务,每个进程一个客户.客户数目的唯一限制是操作系统对以其名义 ...

  8. UNIX网络编程——非阻塞connect:时间获取客户程序

    #include "unp.h" int connect_nonb(int sockfd, const SA *saptr, socklen_t salen, int nsec) ...

  9. Android原生嵌入React Native

    1.首先集成的项目目录 我使用的是直接按照react-native init Project 的格式来导入的,也就是说,我的Android项目目录是跟node_modules是在一个目录下的. 我们i ...

  10. 【Android】自定义ListView的Adapter报空指针异常解决方法

    刚刚使用ViewHolder的方法拉取ListView的数据,但是总会报异常.仔细查看代码,都正确. 后来打开adapter类,发现getView的返回值为null. 即return null. 将n ...