【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 ...
随机推荐
- Hive伪分布式下安装
本安装过程只作为个人笔记用,非标准教程,请酌情COPY.:-D Hive下载 下载之前,需先查看兼容的Hadoop版本,并安装hadoop,参考 http://www.cnblogs.com/yong ...
- iOS开发 跳转场景的三种方式
iOS开发 跳转场景的三种方式 2012年10月17日, 15:32 假设A跳转到B,三种方法:1.按住ctrl键,拖动A上的控件(比如说UIButton)到B上,弹出菜单,选择Modal.不需要写任 ...
- oc/object-c/ios哪种遍历NSArray/NSDictionary方式快?测试报告
做app的时候,总免不了要多次遍历数组或者字典.究竟哪种遍历方式比较快呢?我做了如下测试:首先定义测试用宏: ? 1 2 3 4 5 6 7 8 9 #define MULogTimeinterval ...
- Circuit translates I2C voltages
This Design Idea explores level-shifting an I2C bus from 5V/ground (positive domain) to ground/–5V ( ...
- Spring EL hello world实例
Spring EL与OGNL和JSF EL相似,计算评估或在bean创建时执行.此外,所有的Spring表达式都可以通过XML或注解. 在本教程中,我们将学习如何使用Spring表达式语言(SpEL) ...
- 关于DSP的GPIO的输入输出设置
DSP 28335 的 GPIO的输入设置: GPIO的输入设置时,除了将此GPIO设置为输入之外,还需要将此GPIO口的电平拉高 //RXD3 GpioCtrlRegs.GPBMUX2.bit.GP ...
- 实现windows操作系统和VB下Linux虚拟操作系统相互传取文件方式总结
在windows上执行虚拟机跑的是Linux的操作系统,怎样才干在不同的操作系统之间传递文件呢? 这是本人切身体会到的,假设你没有好的方法的话.确实非常痛苦.下面是我个人的方法总结: 方法一.很好用的 ...
- 无线AP和无线路由器区别wifi热点
转自:http://network.51cto.com/art/201310/413327.htm 就像很多用户很容易混淆无线上网卡和无线网卡一样,很多用户也分不清无线AP和无线路由,小峰便是其中的一 ...
- Java垃圾回收精粹 — Part1
Java垃圾回收精粹分4个部分,本篇是第1部分.在第1部分里介绍了权衡点.对象生命周期以及全局暂停事件. 串行.并行.并发.CMS.G1.年轻代(Young Gen).新生代(New Gen).旧生代 ...
- 仿LOL项目开发第三天
仿LOL项目开发第二天 by草帽 昨个我们已经实现了下载功能,但是发现没有,下载的包是压缩的,没有解压开,那么Unity是识别不了的. 所以今个我们来讲讲如何实现解压文件. 还记得吗,我们在Downl ...