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 shortest path from the root node down to the nearest leaf node.
分析
求二叉树的最小深度:根节点到最近叶子节点的路径长度。
同样采用递归的思想:
- 当根节点为空,返回0;
- 当根节点为唯一的二叉树节点时,返回1;
- 否则,求解并返回 最小(非空,保证最终到达叶节点)左右子树深度 + 1;
AC代码
class Solution {
public:
int minDepth(TreeNode* root) {
if (!root)
return 0;
//独立的根节点
else if (!root->left && !root->right)
return 1;
else{
int left_depth, right_depth;
if (root->left)
left_depth = minDepth(root->left);
else
left_depth = INT_MAX;
if (root->right)
right_depth = minDepth(root->right);
else
right_depth = INT_MAX;
return min(left_depth, right_depth) + 1;
}
}
};
LeetCode(111) Minimum Depth of Binary Tree的更多相关文章
- 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 l ...
- [LeetCode]题解(python):111 Minimum Depth of Binary Tree
题目来源 https://leetcode.com/problems/minimum-depth-of-binary-tree/ Given a binary tree, find its minim ...
- 【一天一道LeetCode】#111. Minimum Depth of Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- 【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 ...
- [LeetCode]题解(python):110 Balanced Binary Tree
题目来源 https://leetcode.com/problems/balanced-binary-tree/ Given a binary tree, determine if it is hei ...
- 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 ...
- LeetCode OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
- 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 ...
随机推荐
- Springboot日志配置探索(主要看logback)(一)
这篇博客是springboot日志配置探索的第一篇,主要讲默认配置下springboot的logback日志框架的配置(即直接使用是怎样的) 首先,是一个SpringBoot的有关日志的说明文档:ht ...
- 18002 Z-Scan 模拟题
18002 Z-Scan 时间限制:1000MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: 不限定 Description Z-Scan is a method ...
- ruby字符串处理
1. str="abc123"puts str[0].chr => a puts str[0] =>a的ascii码 2.中文字符串的正则 ...
- MVC系列学习(十六)-区域的学习
1.查找控制器的过程 1.1调用其他项目中的控制器 a.先到网站根目录下的bin文件夹下,遍历所有的程序集 b.找到以Controller结尾的类 c.再找出其中继承了Controller的类 d.接 ...
- Ajax 提交表单【包括文件上传】
利用js插件实现 <script src="@Url.Content("~/js/layer/jquery.form.min.js")"></ ...
- 详解HTML中的表格标签
详细代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...
- idea 设置加载多个资源文件,显示本地图片
idea 经常只会设置一个资源路径,这个路径就是项目的路径.但是当要加载的文件处于其他位置时,则需要增加虚拟路径的配置. 如图:第一个是项目路径 第二个是图片路径
- Spark-水库抽样-根据抽样率确定每个分区的样本大小
/* * 输入:采样率,待采样的RDD * 输出:每个分区的样本大小(记录数) * 由采样率确定,每个分区的样本大小 */ def findNumPerPartition[T: ClassTag, U ...
- 【Web应用-FTP】FTP 容量显示说明
现象描述 Azure 门户显示的文件系统存储容量跟网站本身的磁盘空间不符. 问题分析 Azure Web 应用的文件系统存储用量和网站本身有关,具体容量如下所示: 但目前门户预览中关于 FTP 的容量 ...
- DRBD+NFS+Keepalived高可用环境
1.前提条件 准备两台配置相同的服务器 2.安装DRBD [root@server139 ~]# yum -y update kernel kernel-devel [root@server139 ~ ...