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. [二次开发]dede文章页面怎样显示作者的头像

    dede在文章页面显示作者仅仅是显示其username,可是假如我想把dede改造成较为社交化的站点.我认为是有必要显示作者的头像的,可是官方并没有相应的模版标签. 在网上看到解决问题的办法基本上是直 ...

  2. 印象笔记Mac端快捷键

  3. 使用Kotlin开发Android应用(I):简介

    Kotlin是一门基于JVM的编程语言,它正成长为Android开发中用于替代Java语言的继承者.Java是世界上使用最多的编程语言之一,当其他编程语言为更加便于开发者使用而不断进化时,Java并没 ...

  4. UI控件之UITableView的协议方法

    <UITableViewDataSource,UITableViewDelegate> //设置表视图的编辑状态 -(void)setEditing:(BOOL)editing anima ...

  5. Python学习进程(6)函数

        函数最重要的目的是方便我们重复使用相同的一段程序.     (1)函数的定义: 函数定义的简单规则: 1.函数代码块以 def 关键词开头,后接函数标识符名称和圆括号(): 2.任何传入参数和 ...

  6. RocketMQ 笔记-转

    Astrotrain概述 Astrotrain是基于阿里巴巴开源项目RocketMQ进行封装的分布式消息中间件系统,提供集群环境下的消息生产和消费功能. RocketMQ介绍 RocketMQ的物理部 ...

  7. python + Streaming框架的MR实践与优化

    Streaming是Hadoop提供的一个可以使用其他编程语言来进行MR编程的API,它使用Unix标准输入输出作为Hadoop和其他编程语言的开发接口,非常轻便.而开发者可以选择自己擅长的编程语言, ...

  8. MVC6 (ASP.NET5) 自定义TagHelper

    1) 在 _ViewImports.cshtml 中引入TagHelper类所在的 Assembly . (注意不是namespace)  : @addTagHelper "*, WebAp ...

  9. Linux VPS实用简单安全配置

    今天,和大家一起来分享VPS最基本的安全配置. 第一.修改SSH端口 VPS默认的SSH端口是22,那些扫描穷举密码的,也势必从22开始,所以,修改22为一个其他的数字,是非常有必要的. 好了,SSH ...

  10. js 元素高度宽度整理

    1.1只读属性 所谓的只读属性指的是DOM节点的固有属性,该属性只能通过js去获取而不能通过js去设置,而且获取的值是只有数字并不带单位的(px,em等),如下: 1)clientWidth和clie ...