当前位置: 首页 > ai >正文

Leetcode 深度优先搜索 (9)

111. 二叉树的最小深度

给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。说明:叶子节点是指没有子节点的节点。

示例 1:
在这里插入图片描述
输入: root = [3,9,20,null,null,15,7]
输出: 2

广度优先搜索(BFS)思路:

  • BFS利用队列进行层序遍历,从根节点开始逐层向下遍历。
  • 每访问到一个节点时,判断其是否为叶子节点(即没有左右子节点)。
  • 一旦遇到第一个叶子节点,当前遍历的层数即为最小深度,可以立即返回。
  • 这种方法保证了最先到达的叶子节点就是距离根节点最近的。
public class Solution {public int minDepth(TreeNode root) {if (root == null) return 0;Queue<TreeNode> queue = new LinkedList<>();queue.offer(root);int depth = 1;while (!queue.isEmpty()) {int size = queue.size();for (int i = 0; i < size; i++) {TreeNode node = queue.poll();// 判断是否为叶子节点if (node.left == null && node.right == null) {return depth;}if (node.left != null) queue.offer(node.left);if (node.right != null) queue.offer(node.right);}depth++;}return depth;}
}

深度优先搜索(DFS)递归思路:

  • 如果根节点为空,返回0。
  • 如果左子树为空,递归计算右子树的最小深度并加1。
  • 如果右子树为空,递归计算左子树的最小深度并加1。
  • 如果左右子树都不为空,返回左右子树最小深度的较小值加1。
    public int minDepth(TreeNode root) {if (root == null) {return 0;}// 如果左子树为空,递归右子树if (root.left == null) {return minDepth(root.right) + 1;}// 如果右子树为空,递归左子树if (root.right == null) {return minDepth(root.left) + 1;}// 左右子树都不为空,取较小值return Math.min(minDepth(root.left), minDepth(root.right)) + 1;}

深度优先搜索(DFS)非递归思路:

  • 使用栈(Stack)来模拟递归过程。
  • 每个栈元素存储一个节点和其当前深度。
  • 从根节点入栈,循环弹栈处理:
    • 如果当前节点是叶子节点,更新最小深度。
    • 否则,将其左右子节点(如果存在)和对应深度入栈。
  • 最终返回最小深度。
public int minDepth(TreeNode root) {if (root == null) return 0;Stack<Pair<TreeNode, Integer>> stack = new Stack<>();stack.push(new Pair<>(root, 1));int minDepth = Integer.MAX_VALUE;while (!stack.isEmpty()) {Pair<TreeNode, Integer> current = stack.pop();TreeNode node = current.getKey();int depth = current.getValue();if (node.left == null && node.right == null) {minDepth = Math.min(minDepth, depth);}if (node.right != null) {stack.push(new Pair<>(node.right, depth + 1));}if (node.left != null) {stack.push(new Pair<>(node.left, depth + 1));}}return minDepth;}// 辅助类,用于存储节点和深度static class Pair<K, V> {private K key;private V value;public Pair(K key, V value) {this.key = key;this.value = value;}public K getKey() { return key; }public V getValue() { return value; }}
http://www.xdnf.cn/news/18195.html

相关文章:

  • MPR多平面重建一:初步实现
  • linux报permission denied问题
  • 【C语言16天强化训练】从基础入门到进阶:Day 4
  • 创建Vue项目的不同方式及项目规范化配置
  • 大数据常见问题分析与解决方案
  • 《SQLAlchemy 2 In Practice》读后感
  • C++开发/Qt开发:单例模式介绍与应用
  • IDEA:控制台中文乱码
  • Redis知识总结
  • 【机器学习深度学习】Ollama、vLLM、LMDeploy对比:选择适合你的 LLM 推理框架
  • MySQL高阶篇-数据库优化
  • 计算机网络模型
  • 企业通讯软件保证内部通讯安全,搭建数字安全体系
  • 建筑行业变革:用Three.js构建BIM数据可视化孪生平台
  • 代码管理平台Gitlab如何通过 ZeroNews 实现远程访问?
  • AI时代SEO关键词优化新策略
  • Redis-缓存-雪崩-持久化、集群、灾备
  • 大数据毕业设计选题推荐-基于Hadoop的电信客服数据处理与分析系统-Spark-HDFS-Pandas
  • Windows 上用 pyenv-win 玩转多版本 Python:安装、国内源、常用命令与版本切换
  • 代码随想录Day57:图论(寻宝prim算法精讲kruskal算法精讲)
  • HT6881:重塑便携式音频体验的高效能功率放大器
  • Paraformer实时语音识别中的碎碎念
  • 将SSL配置迁移到Nacos的步骤
  • HarmonyOS 中的 setInterval的基本使用
  • 分布式机器学习之流水线并行GPipe:借助数据并行来实现模型并行计算
  • 矿物分类系统开发笔记(二):模型训练[删除空缺行]
  • ZooKeeper 一致性模型解析:线性一致性与顺序一致性的平衡
  • VScode ROS文件相关配置
  • 【habitat学习一】Habitat-Lab 配置键文档详解(CONFIG_KEYS.md)
  • 嵌入式开发学习———Linux环境下网络编程学习(三)