【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 ...
随机推荐
- Ubuntu 16.04开启SFTP服务
说明:其实只要安装了SSH服务就已经具备了SFTP功能,这个用普通客户端无法连接,只能用支持SFTP协议的客户端才能连接. FileZilla作为FTP客户端,它也可以连接SFTP,SFTP的监听端口 ...
- CentOS 6.9永久设置静态路由表以及路由表常用设置
一.路由表常用设置: 1.使用route命令添加的路由,机器重启或者网卡重启后路由就失效了,方法: //添加到主机的路由 # route add –host 192.168.1.11 dev eth0 ...
- Apple Developer申请成功
上周日白天,我去申请了Apple Developer.我先是在百度上浏览了一些经验教程,但是点进苹果开发者官网时却发现完全不是那么一回事.盖因它的页面经常在变,如同现在苹果在主推tvOS这个对中国用户 ...
- Windows用WinDbg分析蓝屏dump文件查找原因(转)
WinDbg官方下载: http://msdl.microsoft.com/download/symbols/debuggers/dbg_x86_6.11.1.404.msi http://msdl. ...
- Linux开发环境必备十大开发工具
Linux是一个优秀的开发环境,但是如果没有好的开发工具作为武器,这个环境给你带来的好处就会大打折扣.幸运的是,有很多好用的Linux和开源开发工具供你选择,如果你是一个新手,你可能不知道有哪些工具可 ...
- 如何安装pip、升级pip包。mac下安装包的路径
参考:https://pip.pypa.io/en/stable/installing/ 1.wget -c https://bootstrap.pypa.io/get-pip.py 2.pytho ...
- jenkins的docker
参考:https://store.docker.com/images/jenkins?tab=description https://my.oschina.net/jayqqaa12/blog/633 ...
- c# 使用api函数 ShowWindowAsync 控制窗体
1.需要匯入 System.Runtime.InteropServices 命名空間 2.宣告 ShowWindowAsync 函數 [DllImport("user32.dll" ...
- 清空iframe的内容
document.getElementById("web").contentWindow.document.body.innerText = "";
- Morton Code
莫顿码 ,实现了一维与二维(或多维)的转换.它通过交叉存储两个数的位产生一个数,即莫顿码. 可以应用于为一个整数对产生一个唯一索引. 例如,对于坐标系中的坐标点使用莫顿编码生成的莫顿码,可以唯一索引对 ...