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.

求二叉树的最小深度,额,简单的递归而已,代码如下:

 class Solution {
public:
int minDepth(TreeNode* root) {
if(root = NULL) return ;
int leftDep = minDepth(root->left);
int rightDep = minDepth(root->right);
if(leftDep == && rightDep == ) return ;//叶子节点
if(leftDep == ) leftDep = INT_MAX;
if(rightDep == ) rightDep = INT_MAX;//中间节点
return min(leftDep, rightDep) + ;
}
};

java 版本的代码一样,与上面相同,代码如下所示:

 /**
* 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;
int leftDep = minDepth(root.left);
int rightDep = minDepth(root.right);
if(leftDep == 0 && rightDep == 0)
return 1; //到达这里表明的是叶子节点
if(leftDep == 0) leftDep = Integer.MAX_VALUE;
if(rightDep == 0) rightDep = Integer.MAX_VALUE;
return Math.min(leftDep, rightDep) + 1;
}
}

LeetCode OJ:Minimum Depth of Binary Tree(二叉树的最小深度)的更多相关文章

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

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

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

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

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

  4. [Leetcode] The 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] 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 | 二叉树的最小深度 | Easy

    要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意 ...

  7. 111 Minimum Depth of Binary Tree 二叉树的最小深度

    给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-t ...

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

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

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

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

  10. LeetCode OJ——Minimum Depth of Binary Tree

    http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ 贡献了一次runtime error,因为如果输入为{}即空的时候,出现了c ...

随机推荐

  1. MongoDB学习笔记—windows下安装

    1.登录官网下载安装包 官网下载地址:https://www.mongodb.com/download-center?jmp=nav#community 根据你的系统下载 32 位或 64 位的 .m ...

  2. 从SignalTap II中获取“最真实”的仿真测试向量(ZZ)

         在实际工作中,经常会遇到这样的情况:在硬件调试中采用SignalTap II反复多次编译并最终捕获到问题的原因时,才会发现,原来这个问题是逻辑问题,是可以在仿真环境下发现并快速解决的.先前没 ...

  3. Scrapy框架2

    一.进程.线程.协成 1.进程.线程.协成之间的关系          1.  线程是计算机中最小的工作单元.              2. 进程是提供资源供n个线程使用,即进程是最小的管理单元. ...

  4. Linux基础系列:常用命令(2)

    作业一: 1) 新建用户natasha,uid为1000,gid为555,备注信息为“master” groupadd -g 555 natasha useradd -u 1000 -g 555 -c ...

  5. 小程序学习第二天 认识框架WXML

    一.初级小程序HelloWorld 心得: (1)progect.config.json :app的个性化设置 (2)一个小程序至少包括两个文件 (2.1)app.json 小程序全局配置       ...

  6. Java底层代码实现多文件读取和写入

    需求: "E:/data/"目录下有四个文件夹,如下: 每个文件夹下有几个.csv文件,如下: 将每个文件夹下的.csv文件合并成一个以该文件夹命名的.csv文件. 做法: 找到& ...

  7. linux中搭建docker

    1.通过 vagrant ssh登录虚拟机 2.在虚拟机中通过 yum 命令安装docker 3.通过docker -v检查docker是否安装成功 4.开启docker加速器 curl -sSL h ...

  8. github使用——如何恢复被删去文件。

    首先git删除文件包括以下几种情况 删除本地文件,但是未添加到暂存区: 删除本地文件,并且把删除操作添加到了暂存区: 把暂存区的操作提交到了本地git库: 把本地git库的删除记录推送到了远程服务器g ...

  9. Qt开发UDP

    一.单播 1.声明udp对象 QUdpSocket* udpClient: 2.new出对象 udpClient = new QUdpSocket(this); 3.分配本地地址(如果不分配,使用系统 ...

  10. MongoDB快速入门(二)- 数据库

    创建数据库 MongoDB use DATABASE_NAME 用于创建数据库.该命令如果数据库不存在,将创建一个新的数据库, 否则将返回现有的数据库. 语法 use DATABASE语句的基本语法如 ...