码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode.111. 二叉树的最小深度


    ​​​​​​111. 二叉树的最小深度

    难度:easy

     

     

    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode() {}
    8. * TreeNode(int val) { this.val = val; }
    9. * TreeNode(int val, TreeNode left, TreeNode right) {
    10. * this.val = val;
    11. * this.left = left;
    12. * this.right = right;
    13. * }
    14. * }
    15. */
    16. class Solution {
    17. public int minDepth(TreeNode root) {
    18. // int minDepth = Integer.MAX_VALUE;
    19. if (root == null) {
    20. return 0;
    21. }
    22. Queue queue = new LinkedList<>();
    23. queue.offer(root);
    24. int depth = 0;
    25. while (!queue.isEmpty()) {
    26. int size = queue.size();
    27. // 层数+1
    28. depth++;
    29. for (int i = 0; i < size; i++) {
    30. TreeNode node = queue.poll();
    31. // 一个节点不存在左节点和右节点就可以计算深度;
    32. if (node.left == null && node.right == null) {
    33. // minDepth = Math.min(minDepth, depth);
    34. return depth;
    35. }
    36. if (node.left != null) {
    37. queue.offer(node.left);
    38. }
    39. if (node.right != null) {
    40. queue.offer(node.right);
    41. }
    42. }
    43. }
    44. return minDepth;
    45. }
    46. }

    在leetcode看到一个不错的题解,通过自行封装QueueNode加入depth信息,随时可以获得最短的深度。

    1. class Solution {
    2. class QueueNode {
    3. TreeNode node;
    4. int depth;
    5. public QueueNode(TreeNode node, int depth) {
    6. this.node = node;
    7. this.depth = depth;
    8. }
    9. }
    10. public int minDepth(TreeNode root) {
    11. if (root == null) {
    12. return 0;
    13. }
    14. Queue queue = new LinkedList();
    15. queue.offer(new QueueNode(root, 1));
    16. while (!queue.isEmpty()) {
    17. QueueNode nodeDepth = queue.poll();
    18. TreeNode node = nodeDepth.node;
    19. int depth = nodeDepth.depth;
    20. if (node.left == null && node.right == null) {
    21. return depth;
    22. }
    23. if (node.left != null) {
    24. queue.offer(new QueueNode(node.left, depth + 1));
    25. }
    26. if (node.right != null) {
    27. queue.offer(new QueueNode(node.right, depth + 1));
    28. }
    29. }
    30. return 0;
    31. }
    32. }

  • 相关阅读:
    数字化技术推动产业升级,B2B电商交易管理系统助力传统企业重塑竞争力
    前端面试中Vue的有经典面试题三
    Java代码审计15之Apache log4j2漏洞
    继电器在信号系统中的应用
    案例拆解丨逆势增长,瑞幸是怎么打好私域这张牌的?
    9、软件包管理
    1187 Candy,xtu
    H5移动端便捷兼容测试方式
    灵活部署的CRM系统和传统软件的区别?
    常用linux命令
  • 原文地址:https://blog.csdn.net/weixin_45867071/article/details/126767954
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号