Minimum Depth of Binary Tree

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.

Maximum Depth of Binary Tree对照看

解法一:深度优先遍历

借助栈进行深度优先遍历(DFS),栈中存放的就是从根到当前节点的路径。

当遇到叶节点的时候,把当前栈中路径长度与minD比较,取较小值赋给minD。

返回minD即最小深度。

/**
* 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 ;
stack<TreeNode*> stk;
int ret = INT_MAX;
unordered_map<TreeNode*, bool> visited;
stk.push(root);
visited[root] = true;
while(!stk.empty())
{
TreeNode* top = stk.top();
if(!top->left && !top->right)
//leaf
ret = min(ret, (int)stk.size()); if(top->left && visited[top->left] == false)
{
stk.push(top->left);
visited[top->left] = true;
continue;
}
if(top->right && visited[top->right] == false)
{
stk.push(top->right);
visited[top->right] = true;
continue;
} stk.pop();
}
return ret;
}
};

解法二:递归

注意:定义中需要到达叶节点,因此空的子节点不计算在内。

/**
* 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 ;
else if(root->left && !root->right)
return +minDepth(root->left);
else if(!root->left && root->right)
return +minDepth(root->right);
else
return +min(minDepth(root->left), minDepth(root->right));
}
};

【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)的更多相关文章

  1. 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  2. 【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 ...

  3. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

    Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the ...

  4. 【一天一道LeetCode】#111. Minimum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度

    求二叉树的最小深度: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  6. 【leetcode❤python】 111. Minimum Depth of Binary Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  7. LeetCode OJ 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 ...

  8. 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

  9. 【LeetCode】104 - 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. ADC Power Supplies

    http://www.planetanalog.com/author.asp?section_id=3041&doc_id=563055 Jonathan Harris, Product Ap ...

  2. hdu4035之经典慨率DP

    Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submi ...

  3. VS 2017 取消结构参考线的显示

    Visual studio 中的结构参考线如下所示 其可以通过如下方式取消:

  4. hashmap实现原理2

    put和get都首先会调用hashcode方法,去查找相关的key,当有冲突时,再调用equals(这也是为什么刚开始就重温hashcode和equals的原因)! HashMap基于hashing原 ...

  5. openfire Hazelcast插件集群配置

    原文:http://blog.csdn.net/frankcheng5143/article/details/48708899 注意虽然hazelcast 官方已经有了3.5.2版本,但是openfi ...

  6. Virtualbox虚拟机安装CentOS 6.5图文详细教程

    http://blog.csdn.net/risingsun001/article/details/37934975

  7. 查找链表中倒数第k个结点

    题目:输入一个单向链表,输出该链表中倒数第k个结点.链表的倒数第0个结点为链表的尾指针.链表结点定义如下: struct ListNode { int m_nKey; ListNode* m_pNex ...

  8. Orchard运用 - 定制呈现最新博客文章

    每个博客系统为了吸引更多访问量,一般都会在首页或侧边栏列举一些最新文章/随笔以获取更多点击.其实也就是查询出最新的几篇文章并按照简练的方式呈现,比如一般都只有标题及其对应的链接,有时也会标注一下作者和 ...

  9. 根据不同浏览器使用不同的css文件

    代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  10. #pragma详细解释(一)

    #pragma详细解释 #pragma详细解释(一) 2010-04-18 14:21:00|  分类: 默认分类 |  标签: |字号大中小订阅     在#Pragma是预处理指令它的作用是设定编 ...