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.

递归的解题思路:

递归当前结点,分一下四种情况考虑:①结点为空时返回0;②结点没有右子树时,返回左子树最小值+1;③结点没有左子树时,返回右子树最小值+1;④当结点双子齐全时,返回左右子树的最小值+1;

注意:

1、注意判断“只有左子树”或“只有右子树”的情况,不要暴力的直接返回左/右子树最小值,因为有可能不存在;

2、省去了“当结点为叶子结点,则返回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)
return ; int minLeft = minDepth(root->left);
int minRight = minDepth(root->right); if (minLeft == )
return minRight + ;
else if (minRight == )
return minLeft + ;
else return min(minLeft,minRight) + ;
}
};

【★】迭代的解法:

用按层遍历二叉树的思想,当遍历到第一个叶子结点时,输出此时树的深度,则为最小depth。

用数组保存待遍历的树,设置双指针,一个指向访问当层开始的节点,一个指向访问当层结束节点的下一个位置。

 class Solution {
public:
int minDepth(TreeNode *root) {
vector<TreeNode *> treeVec;
treeVec.push_back(root);
int cur = ;
int end;
int depth = ; if (!root)
return ; while (cur < treeVec.size()) {
depth++;
end = treeVec.size();
while (cur < end) {
if (!treeVec[cur]->left && \
!treeVec[cur]->right) {
return depth;
} if (treeVec[cur]->left)
treeVec.push_back(treeVec[cur]->left); if (treeVec[cur]->right)
treeVec.push_back(treeVec[cur]->right); cur++;
}
}
}
};

附录:

按层遍历二叉树

【Leetcode】【Easy】Minimum Depth of Binary Tree的更多相关文章

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

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

  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练习题】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】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 ...

  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. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

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

  7. leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree

    1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...

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

  9. LeetCode My Solution: Minimum Depth of Binary Tree

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

  10. 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 ...

随机推荐

  1. css 之 BFC

    1,定义 BFC为块级格式化上下文,也就是一块区域内的封闭空间,里面元素无论怎么样,都不会影响外部元素. 2,触发条件 html 根元素 display的值为 inline-block.table-c ...

  2. PIE SDK创建掩膜

      1.算法功能简介 图像掩膜(Mask)用选定的图像.图形或物体,对处理的图像(全部或局部)进行遮挡,来控制图像处理的区域或处理过程.掩膜是一种图像滤镜的模板,实用掩膜经常处理的是遥感图像.当提取道 ...

  3. 使用Tomcat搭建一个可以通过公网访问的服务器(转)

    转自:http://wenku.baidu.com/link?url=kGLhiO1xiiepXa9Q2OJDmm6Zr8dQmpSYYPVTFmc3CZtD6Z7HvFi2miCYDiQdTYF2T ...

  4. JS 判断节点类型

    节点类型的分类 节点类型 说明 值 元素节点 每一个HTML标签都是一个元素节点,如 <div> . <p>.<ul>等 1 属性节点 元素节点(HTML标签)的属 ...

  5. 转帖 css的块元素、内联元素、内联块元素、display属性、浮动、定位

    块元素 块元素,也可以称为行元素,布局中常用的标签如:div.p.ul.li.h1~h6.dl.dt.dd等等都是块元素,它在布局中的行为:1.支持全部的样式.2.如果没有设置宽度,默认的宽度为父级宽 ...

  6. PLSQL Developer 客户端工具的安装

    安装之前先把客户端工具instantclient_12_1拷贝到一个没有中文和空格的目录中去, 比如我直接放到了D:\tools下面.注意这里需要的是32bit的. 下面开始安装PLSQL Devel ...

  7. org.hibernate.QueryException: duplicate alias: r hibernate别名重复问题解决

    今天做项目的过程中发现,多表查询的时候如果使用hibernate的DetachedCriteria离线查询方式的时候, 在多表关联的时候我们需要使用别名的方式去实现. 但是代码运行的过程中抛出了下面的 ...

  8. hrb——开锁魔法I——————【规律】

    解题思路:从1到n的倒数之和. #include<stdio.h> #include<string.h> #include<algorithm> using nam ...

  9. Prometheus TSDB分析

    Prometheus TSDB分析 概述 Prometheus是著名开源监控项目,其监控任务调度给具体的服务器,该服务器到目标上抓取监控数据,然后保存在本地的TSDB中.自定义强大的PromQL语言查 ...

  10. 【Linux】linux文件夹打包命令

    .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ---------------------- ...