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. C. Connect Three Round #528 (Div. 2)【曼哈顿距离】

    一.题面 题目链接 二.分析 这题的关键是要确定一个点是从三个点出发的交汇点,其他的只要结合曼哈顿距离的定义即可明白.因为是三个点,这个交汇点的坐标分别对应的就是x,y值的中值.然后一个小技巧就是曼哈 ...

  2. hdu3966 Aragorn's Story 树链剖分

    题目传送门 题目大意: 有n个兵营形成一棵树,给出q次操作,每一次操作可以使两个兵营之间的所有兵营的人数增加或者减少同一个数目,每次查询输出某一个兵营的人数. 思路: 树链剖分模板题,讲一下树链剖分过 ...

  3. UVALive - 6952 DP 分段/隔板

    题意:商品总价按四舍五入计算,n个物品最多可分\(d+1\)段,求最小代价 \(dp[i][j]\):\(j\)个物品分\(i\)段 注意一个技巧是只在需要分出新的段时才四舍五入(旧段结算),这样就避 ...

  4. PHP_$_SERVER中QUERY_STRING,REQUEST_URI的用法

    $_SERVER存储当前服务器信息,其中有几个值如 $_SERVER["QUERY_STRING"], $_SERVER["REQUEST_URI"], $_S ...

  5. pymongodb的使用和一个腾讯招聘爬取的案例

    一.在python3中操作mongodb 1.连接条件 安装好pymongo库 启动mongodb的服务端(如果是前台启动后就不关闭窗口,窗口关闭后服务端也会跟着关闭) 3.使用 import pym ...

  6. 基于memcache的缓存机制的6个指令

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...

  7. Navicat Premium v12.0.23.0 破解教程x86,x64通用,手动破解

    教程来源于:吾爱破解网站 ----------更新线----------- 2018.01.23 Navicat Premium v12.0.23.0 测试破解依然有效 ----------更新线-- ...

  8. JavaScript中有var和没var的区别

    Js中的变量声明的作用域是以函数为单位,所以我们经常见到避免全局变量污染的方法是 (function(){ // ... })(); 在函数内部,有var和没var声明的变量是不一样的.有var声明的 ...

  9. Springboot - 集成 JPA

    1.什么是 JPA? JPA就是Java Persistence API的意思,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. 2. JPA 具有什么优 ...

  10. C++编程规范(摘记)

    C++编程规范 函数的参数 输入使用const T&, 输出使用指针 函数的返回类型 如果返回引用, 则返回的对象应该是属性, 因为这个涉及到了生命周期 尽量不返回, 而是通过参数列表中的输出 ...