当前位置:网站首页>二叉树——111. 二叉树的最小深度
二叉树——111. 二叉树的最小深度
2022-07-25 23:55:00 【向着百万年薪努力的小赵】
1 题目描述
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。
2 题目示例

示例 2:
输入:root = [2,null,3,null,4,null,5,null,6]
输出:5
3 题目提示
树中节点数的范围在 [0, 105] 内
-1000 <= Node.val <= 1000
4 思路
方法一:深度优先搜索
首先可以想到使用深度优先搜索的方法,遍历整棵树,记录最小深度。
对于每一个非叶子节点,我们只需要分别计算其左右子树的最小叶子节点深度。这样就将一个大问题转化为了小问题,可以递归地解决该问题。
复杂度分析
时间复杂度:O(N),其中N是树的节点数。对每个节点访问一次。
·空间复杂度:O(H),其中H是树的高度。空间复杂度主要取决于递归时栈空间的开销,最坏情况下,树呈现链状,空间复杂度为O(N)。平均情况下树的高度与节点数的对数正相关,空间复杂度为O(log N)。
方法二:广度优先搜索
同样,我们可以想到使用广度优先搜索的方法,遍历整棵树。当我们找到一个叶子节点时,直接返回这个叶子节点的深度。广度优先搜索的性质保证了最先搜索到的叶子节点的深度—定最小。
复杂度分析
时间复杂度:O(N),其中N是树的节点数。对每个节点访问一次。
·空间复杂度:O(N),其中N是树的节点数。空间复杂度主要取决于队列的开销,队列中的元素个数不会超过树的节点数。
5 我的答案
方法一:深度优先搜索
class Solution {
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
int min_depth = Integer.MAX_VALUE;
if (root.left != null) {
min_depth = Math.min(minDepth(root.left), min_depth);
}
if (root.right != null) {
min_depth = Math.min(minDepth(root.right), min_depth);
}
return min_depth + 1;
}
}
方法二:广度优先搜索
class Solution {
class QueueNode {
TreeNode node;
int depth;
public QueueNode(TreeNode node, int depth) {
this.node = node;
this.depth = depth;
}
}
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
Queue<QueueNode> queue = new LinkedList<QueueNode>();
queue.offer(new QueueNode(root, 1));
while (!queue.isEmpty()) {
QueueNode nodeDepth = queue.poll();
TreeNode node = nodeDepth.node;
int depth = nodeDepth.depth;
if (node.left == null && node.right == null) {
return depth;
}
if (node.left != null) {
queue.offer(new QueueNode(node.left, depth + 1));
}
if (node.right != null) {
queue.offer(new QueueNode(node.right, depth + 1));
}
}
return 0;
}
}
边栏推荐
猜你喜欢

What is inode? It will help you understand inode and what help inode provides when creating and deleting files.

S4/hana ME21N create Po output control message button missing solution (switch EDI output mode brf+ to Nast mode)

SQLZOO——Nobel Quiz

赋值时'1和'b1有什么区别

Article 75: writing skills of academic papers

ShardingSphere数据分片

BOM 浏览器对象模型

Part 74: overview of machine learning optimization methods and superparameter settings

Leetcode 0919. complete binary tree inserter: array representation of complete binary tree

Redis basic data type (string/list/set/hash/zset)
随机推荐
模式之固定与交替顺序执行
Loading process such as reflection
Taobao flexible.js file realizes flexible layout
[intelligence question] interview intelligence question
[day.2] Joseph Ring problem, how to use arrays to replace circular linked lists (detailed explanation)
Fixed and alternate sequential execution of modes
[nodejs] nodejs create a simple server
S4/HANA MM & SD EDI基于NAST的集成配置(ORDERS, ORDRSP, DESADV, INVOIC)
Using jetpack libraries in applications
红娘的话
The GUI interface of yolov3 (simple, image detection)
Lua脚本编写Wireshark插件解析第三方私有协议
意向不到的Dubug妙招
R language installation tutorial | graphic introduction is super detailed
浅识 OWASP
Key features and application trends of next generation terminal security management
Practical experience of pair programming
死信队列 和消息TTL过期代码
Storage of data in memory
Exercise (3) create a list set (both ArrayList and LinkedList)