当前位置:网站首页>leetcode:111. 二叉树的最小深度
leetcode:111. 二叉树的最小深度
2022-07-01 09:42:00 【uncle_ll】
111. 二叉树的最小深度
来源:力扣(LeetCode)
链接: https://leetcode.cn/problems/minimum-depth-of-binary-tree
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:2

示例 2:
输入:root = [2,null,3,null,4,null,5,null,6]
输出:5

提示:
- 树中节点数的范围在 [ 0 , 1 0 5 ] [0, 10^5] [0,105] 内
- − 1000 < = N o d e . v a l < = 1000 -1000 <= Node.val <= 1000 −1000<=Node.val<=1000
解法
递归:从根节点开始,如果该节点为空,则返回0;如果不为0,递归调用查找左右子树的深度,最终结果为根节点的1层+ min(left, right), 这里要注意,左右子节点一个为空的话,是需要继续往非空的节点往下走
- 叶子节点的定义是左孩子和右孩子都为 null 时叫做叶子节点
- 当 root 节点左右孩子都为空时,返回 1
- 当 root 节点左右孩子有一个为空时,返回不为空的孩子节点的深度
- 当 root 节点左右孩子都不为空时,返回左右孩子较小深度的节点值
bfs循环: bfs层序遍历,如果遍历到某个节点左右子节点都为空时候,即为该树的最小深度
代码实现
递归
python实现
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root:
return 0
if not root.left and not root.right:
return 1
if not root.left:
return 1 + self.minDepth(root.right)
if not root.right:
return 1 + self.minDepth(root.left)
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))
c++实现
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == nullptr)
return 0;
if (root->left == nullptr && root->right == nullptr)
return 1;
if (root->left == nullptr)
return 1 + minDepth(root->right);
if (root->right == nullptr)
return 1 + minDepth(root->left);
return 1 + min(minDepth(root->left), minDepth(root->right));
}
};
复杂度分析
- 时间复杂度: O ( N ) O(N) O(N) 每个节点都遍历一次
- 空间复杂度: O ( h e i g h t ) O(height) O(height) height是二叉树的高度,递归需要栈空间,栈空间取决于递归的深度
BFS循环
python实现
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def minDepth(self, root: TreeNode) -> int:
if not root:
return 0
# bfs
dq = []
dq.append((1, root))
while dq:
depth, node = dq.pop(0)
if not node.left and not node.right:
return depth
if node.left:
dq.append([depth+1, node.left])
if node.right:
dq.append([depth+1, node.right])
return depth
c++实现
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == nullptr)
return 0;
int depth = 1;
deque<pair<TreeNode*, int>> dq;
dq.push_back({
root, 1});
while (dq.size() != 0) {
TreeNode* node = dq.front().first;
int depth = dq.front().second;
dq.pop_front();
if (node->left == nullptr && node->right == nullptr)
return depth;
if (node->left != nullptr)
dq.push_back({
node->left, depth+1});
if (node->right != nullptr)
dq.push_back({
node->right, depth+1});
}
return depth;
}
};
复杂度分析
- 时间复杂度: O ( N ) O(N) O(N)
- 空间复杂度: O ( N ) O(N) O(N)
参考
边栏推荐
- 电脑USB、HDMI、DP各种接口及速度
- 富文本实现插值
- js this丢失问题分析 及 解决方案
- Some tools used in embedded development
- Unity tips for reducing the amount of code -- empty protection extension
- Change password of MySQL version 5.7 and 8.0
- MySQL interception_ MySQL method for intercepting strings [easy to understand]
- sql语句修改字段类型「建议收藏」
- 渗透常用工具-Goby
- 新数据库时代,不要只学 Oracle、MySQL
猜你喜欢

全球基金和资管的股票建仓率达到15年内新低

新数据库时代,不要只学 Oracle、MySQL

JS scope chain and closure

Flinkv1.13 implementation of financial anti fraud cases

ESP8266 FreeRTOS开发环境搭建

JS原型链

The latest masterpiece of Alibaba, which took 182 days to produce 1015 pages of distributed full stack manual, is so delicious

项目必用的全局异常处理器,你学会了吗

IPv6 learning notes

Tearful eyes, it's not easy to change jobs. Three rounds of interviews, four hours of soul torture
随机推荐
PHP 字符串与二进制相互转换
Comparison between Oracle JDK and openjdk
Using closures to implement private variables
SQL learning notes (01) - basic knowledge of database
Clickhouse: Test on query speed of A-share minute data [Part 2]
The market is relatively weak recently
项目采购管理
我喜欢两个男人。。。
button按钮清除边框
[fxcg] large scale job hopping may be one of the driving forces behind the soaring inflation in the United States
Eat a rich woman's melon...
Analysis and solution of JS this loss
SQL学习笔记(02)——数据库表操作
Computer USB, HDMI, DP various interfaces and speeds
Introduction to mt7628k eCos development
SQL学习笔记(01)——数据库基本知识
PHP merges multiple arrays. The same element takes the intersection of different elements to form an array
SQL学习笔记(03)——数据约束关系
Fried money, lost 10million.
Precautions for lvgl v8.2 string display on keil MDK (take little bear pie as an example)