要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离。

结题思路:和找最大距离不同之处在于:找最小距离要注意(l<r)? l+1:r+1的区别应用,因为可能存在左右子树为空的情况,此时值就为0,但显然值是不为0的(只有当二叉树为空才为0),所以,在这里注意一下即可!

代码如下:

 struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x): val(x),left(NULL), right(NULL) {}
}; int minDepth(TreeNode *root)
{
if (NULL == root)
return ;
int l = minDepth(root->left);
int r = minDepth(root->right);
if (!l)
return r+;
if (!r)
return l+;
return (l<r)?l+1:r+1;
}

LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy的更多相关文章

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

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

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

  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】Minimum Depth of Binary Tree 二叉树的最小深度 java

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

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

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

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

  7. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

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

  9. [Leetcode] Maximum depth of binary tree二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

随机推荐

  1. JAVA四则运算(读写文件)

    完成时间:17:10 package 四则运算试题; import java.io.BufferedReader; import java.io.PrintStream; import java.ut ...

  2. linux下配置redis4.0.2主从复制以及高可用

    一.环境 三台服务器分别为: 172.28.18.75/172.28.18.103/172.28.18.104 在三台服务器上分别部署一个redis节点以及一个sentinel节点 二.主从复制配置 ...

  3. Numpy三维数组的转置与交换轴

    二维数组的转置应该都知道,就是行列交换 而在numpy中也可以对三维数组进行转置,np.T 默认进行的操作是将0轴与2轴交换 本文主要对三位数组轴交换的理解上发表本人的看法. a = np.array ...

  4. matplotlib 坑

    1 archlinux里安装好matplotlib之后一定要安装python-cario pacman -S python-cairo

  5. 《CSAPP》虚拟存储器

    虚拟存储器与物理存储器 虚拟存储器(VM)被组织为一个由存放在磁盘上的N个连续的字节大小的单元组成的数组.每一个字节都有一个唯一的虚拟地址,这个唯一的虚拟地址作为数组的索引.磁盘上的数组内容被缓存在主 ...

  6. document.getElementById(“id”)与$("#id")的区别

    document.getElementById("id")可以直接获取当前对象, jQuery利用$("#id")获取的是一个[object Object],需 ...

  7. Spring MVC请求处理流程

    从web.xml中 servlet的配置开始, 根据servlet拦截的url-parttern,来进行请求转发   Spring MVC工作流程图   图一   图二    Spring工作流程描述 ...

  8. dump、load和dumps、loads的区别

    dump: 将dict(字典)转换为str(字符串),并写入json文件中. load: 用于从json文件中读取数据 运行结果: dumps: 将dict(字典)转换为str(字符串). 运行结果: ...

  9. python的魔术方法

    什么叫魔术方法: 在python中定义以双下划线开头,有一些python自定义的函数,并且以双下划线为结尾的函数叫做魔法函数 class Company(object): def __init__(s ...

  10. Legend 图例

    1.添加图例 >>> import matplotlib.pyplot as plt >>> import numpy as np >>> x = ...