LeetCode第111题:二叉树的最小深度
问题描述
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回它的最小深度 2.
解题思路
刚开始想得很简单,不就是类比求树的深度,空树返回0,非空树返回左右子树最小深度+1就ok吗?
如果你这么想,那真的too young too simple,sometimes naive。
因为很明显这种思路忽略了根节点只有左子树或者右子树的情况,此种情况下树的最小深度按第一种方法 求得为1,
但是题目所求的是到叶子节点啊同学们,都为nullptr了哪里还叫叶子节点?
正确的递归思路如下:
空树返回0;
左右子树不为空,返回左子树最小深度和右子树最小深度的较小值+1;
左子树不为空,返回左子树最小深度+1;
右子树不为空,返回右子树最小深度+1;
左右子树均为空,返回1;
C++代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
//空指针深度为0
if(root==nullptr)
return 0;
//左右子树均不为空
//左右最小深度+1
if(root->left!=nullptr&&root->right!=nullptr){
int leftDepth=minDepth(root->left);
int rightDepth=minDepth(root->right);
return leftDepth<rightDepth?leftDepth+1:rightDepth+1;
}
//左子树不为空,右子树为空
//左子树最小深度+1
if(root->left!=nullptr&&root->right==nullptr){
return minDepth(root->left)+1;
}
//左子树为空,右子树不为空
//右子树最小深度+1
if(root->left==nullptr&&root->right!=nullptr){
return minDepth(root->right)+1;
}
//左右子树均为空
//返回1
if(root->left==nullptr&&root->right==nullptr){
return 1;
}
}
};
运行结果
LeetCode第111题:二叉树的最小深度的更多相关文章
- [LC]111题 二叉树的最小深度 (递归)
①题目 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15 ...
- 【leetcode 简单】第二十七题 二叉树的最小深度
给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...
- 【LeetCode】111. 二叉树的最小深度
111. 二叉树的最小深度 知识点:二叉树,递归 题目描述 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明:叶子节点是指没有子节点的节点. 示例 输入 ...
- Java实现 LeetCode 111 二叉树的最小深度
111. 二叉树的最小深度 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,nu ...
- leetcode 111. 二叉树的最小深度
题目描述: 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null, ...
- Leecode刷题之旅-C语言/python-111二叉树的最小深度
/* * @lc app=leetcode.cn id=111 lang=c * * [111] 二叉树的最小深度 * * https://leetcode-cn.com/problems/minim ...
- [LeetCode] 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 二叉树的最小深度
计算二叉树的最小深度.最小深度定义为从root到叶子节点的最小路径. public class Solution { public int run(TreeNode root) { if(root = ...
- 【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度
求二叉树的最小深度: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- LeetCode OJ:Minimum Depth of Binary Tree(二叉树的最小深度)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
随机推荐
- [基本操作] Mobius 反演, Dirichlet 卷积和杜教筛
Dirichlet 卷积是两个定义域在正整数上的函数的如下运算,符号为 $*$ $(f * g)(n) = \sum_{d|n}f(d)g(\frac{n}{d})$ 如果不强调 $n$ 可简写为 $ ...
- Centos 7 安装 Python3.7
目录 下载Python Python安装 遇到问题 错误: configure: error: no acceptable C compiler found in $PATH 错误: can't de ...
- HDFS常用的Java Api详解
转自:http://blog.csdn.net/michaelwubo/article/details/50879832 一.使用Hadoop URL读取数据 package hadoop; impo ...
- LeetCode 340. Longest Substring with At Most K Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...
- AngularJs1使用中出现错误 Error: [ng:areq]
1.没有对应的控制器 2.有控制器但是路径没有配对
- 拨打电话demo
- (IBAction)btnClick:(id)sender { UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:nil d ...
- Poj 3253 Fence Repair(哈夫曼树)
Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- asp.net过滤HTML标签,只保留换行与空格
自己从网上找了一个过滤HTML标签的方法,我也不知道谁的才是原创的,反正很多都一样.我把那方法复制下来,代码如下: /// <summary> /// 去除HTML标记 /// </ ...
- 第九课 go的循环语句
1 for语句的三种形式 /* for 循环 */ ; a < ; a++ { fmt.Printf("a 的值为: %d\n", a) } var a, b int = 1 ...
- Rails上传文件
1.view <%= form_tag({:method =>"post",:controller =>"welcome",:action=& ...