当前位置:网站首页>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)
参考
边栏推荐
- Module 9: design e-commerce seckill system
- mysql截取_mysql截取字符串的方法[通俗易懂]
- How did the data center change from "Britney Spears" to "Mrs. cow"?
- ESP8266 FreeRTOS开发环境搭建
- 遇到女司机业余开滴滴,日入500!
- 新数据库时代,不要只学 Oracle、MySQL
- Upload labs for file upload - white box audit
- Concept of digital currency
- SQL learning notes (02) - database table operation
- [fxcg] large scale job hopping may be one of the driving forces behind the soaring inflation in the United States
猜你喜欢
京东与腾讯续签三年战略合作协议;起薪涨至26万元!韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条...
微信表情符号写入判决书,你发的OK、炸弹都可能成为“呈堂证供”
Finally, someone made it clear what DRAM and NAND flash are
CSDN's one-stop cloud service is open for internal testing, and new and old users are sincerely invited to grab the fresh
The latest masterpiece of Alibaba, which took 182 days to produce 1015 pages of distributed full stack manual, is so delicious
持续进阶,软通动力稳步推动云智能战略
谁拥有穿越周期的眼光?
我喜欢两个男人。。。
Cortex M4 systick details
Some tools used in embedded development
随机推荐
华为帐号多端协同,打造美好互联生活
If you meet a female driver and drive didi as an amateur, you can earn 500 a day!
JS prototype inheritance can only inherit instances, not constructors
Some tools used in embedded development
线程基础知识
Ubuntu系统安装与配置MySQL
Swag init error: cannot find type definition: response Response
JS scope chain and closure
睡了二哥。。。
Concept of digital currency
MT7628K eCos开发入门
京东与腾讯续签三年战略合作协议;起薪涨至26万元!韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条...
Eat a rich woman's melon...
ES6 decoupling top-level objects from windows
Floyd repeat
SQL learning notes (02) - database table operation
ESP8266 FreeRTOS开发环境搭建
微信表情符号写入判决书,你发的OK、炸弹都可能成为“呈堂证供”
Hardware midrange project
Closure implementation iterator effect