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. 【python】判断字符串以什么开头或结尾

    项目中用到python判断一个字符串是否以某个字符串结尾,比如,筛选一个目录下所有以.mp4结尾的文件. >>> item = "demo.mp4" >&g ...

  2. PyQt4 调用串口API pySerial API说明

    pySerial API官方介绍链接 http://pyserial.readthedocs.io/en/latest/pyserial_api.html

  3. 剑指offer 面试36题

    面试36题: 题:二叉搜索树与双向链表 题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 解题思路一:由于输入的一个二叉搜索树, ...

  4. 剑指offer 面试40题

    面试40题: 题目:最小的k个数 题:输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 解题代码一: # -*- coding ...

  5. jQuery:自学笔记(2)——jQuery选择器

    jQuery:自学笔记(2)——jQuery选择器 基本选择器 说明 jQuery的基本选择器与CSS的选择器相似: 实例 标签选择器 //使用标签选择器更改字体大小 $(div).css('font ...

  6. node+npm安裝配置

    控制臺輸入node 根據提示安裝   sudo apt-get install -g npm配置淘寶源 npm config set registry https://registry.npm.tao ...

  7. linux 快速清空文件内容

    Tomcat 的catelina.out 如果不配置按照日期产生会在一个文件中产生大量的输出日志. 清除日志如果直接删除catelina.out将无法输出日志.如果想输出日志只能重启Tomcat才会产 ...

  8. 每天一个Linux命令(52)telnet命令

        执行telnet指令开启终端机阶段作业,并登入远端主机.     (1)用法:     用法:  telnet [参数] [主机]     (2)功能:     功能:  telnet命令通常 ...

  9. npm安装出错Unexpected end of input at 1:2307

    执行命令: npm cache clean --force 然后再安装 搞定

  10. 生产&消费者模型

    import queue,threading,time ,random q = queue.Queue() def producer(): count = 1 while count <11: ...