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. Oracle 常用函数大全

    Oracle 11g 常用函数(Functions)详解 目录 ABS. 3 ACOS. 3 ADD_MONTHS. 4 ASCII 4 ASCIISTR. 5 ASIN.. 5 ATAN.. 5 A ...

  2. 经常谈到Oracle的权限,你究竟知道多少

    作者: 三十而立 时间:2009年10月28日 9:41:15 请尊重原创作品.转载请保持文章完整性,并以超链接形式注明原始作者"inthirties(三十而立)" 接着上面谈到的 ...

  3. Django ORM常用字段和参数

    常用字段 AutoField int自增列,必须填入参数 primary_key=True.当model中如果没有自增列,则自动会创建一个列名为id的列. IntegerField 一个整数类型,范围 ...

  4. hdu 1237 简单计算器(栈处理)

    简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  5. telnet出现Connection closed by foreign host

    2018-10-26 执行命令: telnet smtp.exmail.qq.com 出现信息: [root@pengman Desktop]# telnet Tring Connected to 1 ...

  6. 文献综述八:基于JAVA的商品网站的研究

    一.基本信息 标题:基于JAVA的商品网站的研究 时间:2015 出版源:信息技术 文件分类:对java语言的研究 二.研究背景 本文主要介绍了系统的分析,设计和开发的全部过程. 三.具体内容 文献的 ...

  7. select 点击的时候获取 value值

    <select onchange="selectUrl1(this.options[this.options.selectedIndex].value);">      ...

  8. Java面试题搜集

    这里是一些Java面试题,从"程序员小灰"公众号转载过来,备用. 项目介绍 明确项目是做什么的 明确项目的价值.(为什么做这个项目,它解决了用户什么痛点,它带来什么价值?) 明确项 ...

  9. Java入门系列-07-从控制台中接收输入

    这篇文章帮你使用Scanner类从控制台接收输入 从控制台接收字符串 敲一敲: import java.util.Scanner; public class DemoScanner { public ...

  10. BJFU 1549 ——Candy——————【想法题】

    Candy 时间限制(C/C++):1000MS/3000MS          运行内存限制:65536KByte总提交:40            测试通过:20 描述 There are N c ...