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.

Hide Tags

Tree Depth-first Search

 最小深度的意思是从根节点到最近叶节点的最短路径!不是说遇到只有一个孩子的节点就返回。

所以递归的返回条件要分四种情况,第一种自然是NULL节点,直接return 0;第二种是叶节点,return 1;第三种是有一个孩子的节点,返回的是有孩子那边的深度;最后时有两个孩子,返回的是小的深度。

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode *root) {
if(root==NULL)
return ;
if(root->left==NULL && root==NULL)
return ;
int i=minDepth(root->left);
int j=minDepth(root->right);
if(i== || j==)
return max(i,j)+;
return min(i,j)+;
}
};

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: Minimum Depth of Binary Tree 解题报告

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

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

  5. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

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

  6. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  7. 【LeetCode练习题】Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  8. LeetCode My Solution: Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...

  9. 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

随机推荐

  1. 【DM8168学习笔记5】EZSDK目录结构

    EZSDK5.02的目录结构与之前的版本不同,之前的版本各个组件都放在/ezsdk目录下,5.02做了整合. 之前版本:(图片摘自:3.DM816x_1-day_Workshop-Getting_St ...

  2. LUOGU P3708 koishi的数学题

    传送门 解题思路 发现当x+1时,有的x%i会+1,有的会变成0,而变成0的说明是x的约数,就可以nlogn预处理出每个约数的贡献,然后每次用n-约数. 代码 #include<iostream ...

  3. 实现一个函数clone,使JavaScript中的5种主要的数据类型(包括Number、String、Object、Array、Boolean)进行值复制

    实现一个函数clone,可以对JavaScript中的5种主要的数据类型(包括Number.String.Object.Array.Boolean)进行值复制. 1 /** 对象克隆 2 * 支持基本 ...

  4. Cors之带凭据的请求

    带凭据的请求 默认情况下,跨源请求不提供凭据.通过将withCredentials属性设置为true,可以制定某个请求应该发送凭据.

  5. Shell 常用特性

       管道(|) 管道 (|): 将一个命令的输出作为另外一个命令的输入.   管道同样可以在标准输入输出和标准错误输出间做代替工作,这样一来,可以将某一个程序的输出送到另一个程序的输入,其语法如下: ...

  6. .NET框架之---MEF托管可扩展框架

    MEF简介: 今天学习了下MEF框架,MEF,全称Managed Extensibility Framework(托管可扩展框架).MEF是专门致力于解决扩展性问题的框架,MSDN中对MEF有这样一段 ...

  7. SQL Sever实验二 交互式 SQL

    一. 实验目的 1.观察查询结果, 体会 SELECT 语句实际应用: 2.要求学生能够使用 SELECT 语句进行数据库查询. 3. 熟练掌握各种查询的操作方法. 二. 实验准备 1. 完成实验一所 ...

  8. 转: PHP的"::"、"->"和"=>"的区别

    来自:http://blog.csdn.net/wgw335363240/article/details/20453379 1."::" 运算法 该运算符是调用一个类中的静态成员的 ...

  9. CISC和RISC是什么?它们的特点和区别?

    CISC的英文全称为“Complex Instruction Set Computer”,即“复杂指令系统计算机”,从计算机诞生以来,人们一直沿用CISC指令集方式.早期的桌面软件是按CISC设计的, ...

  10. myeclipse设置自动热部署

    MyEclipse中开发网站项目如何设置关联的Tomcat服务器热启动,即修改项目源代码时不需要每次都重启Tomcat 目前在做一个网站项目,使用MyEclipse+Tomcat,每次修改项目源代码时 ...