【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 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)的更多相关文章
- 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- 【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 ...
- 【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 ...
- 【一天一道LeetCode】#111. Minimum Depth of Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度
求二叉树的最小深度: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- 【leetcode❤python】 111. Minimum Depth of Binary Tree
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- 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 ...
- 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...
- 【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 ...
随机推荐
- PAT甲级1049. Counting Ones
PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...
- Windows Sysinternals Suite
Windows Sysinternals Suite 是一套由微软官方免费提供的系统工具集,其中包含了大量超级实的优秀绿色小软件,譬如 Desktops (虚拟桌面).Process Explorer ...
- Cortex-M3 and Cortex-M4 Memory Organization
http://www.mikroe.com/download/eng/documents/compilers/mikropascal/pro/arm/help/memory_organization. ...
- Druid 配置_StatViewServlet配置
https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatViewServlet%E9%85%8D%E7%BD%AE Druid内置提供 ...
- POJ2352【树状数组】
个人NO.1 一开始题意理解有错. 一星星左下边有N颗星星,那它的等级就是N. 一开始理解必须X,Y两个坐标都小于,后来根据样例看了一下只要左下方即可,X,Y坐标都小于等于即可,但不包括星星本身. # ...
- linux查看端口被哪个服务占用的命令
netstat -tunpl | grep 6379
- 【spring boot】启动类启动 错误: 找不到或无法加载主类 com.codingapi.tm.TxManagerApplication 的解决方案
导入的一个外部的spring boot项目,运行启动类,出现错误:找不到或无法加载主类 com.codingapi.tm.TxManagerApplication 解决方案: 将所有错误处理完成后,再 ...
- 第一篇 对Javascript中原型的深入理解
理解原型对象 在Javascript中不管什么时候,仅仅要创建一个新的函数,就会依据一组特定的规则为该函数创建一个prototype属性,这个属性指向函数的原型对象(这个对象的用途是包括能够有特定 ...
- Guava API - FluentIterable Predicate Function Odering Range Splitter
这写API可解决的问题 1. 集合元素的过滤 - FluentIterable Predicate Range Function 1) 先说Predicate<T>,这个相当与一个过滤原则 ...
- Single Number and Single Number II
[] Given an array of integers, every element appears twice except for one. Find that single one. [] ...