码农知识堂 - 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. }

  • 相关阅读:
    OX08B40sensor的规格书
    CORE: Automatic Molecule Optimization using Copy and Refine Strategy(论文解读)
    成为Moonbeam社区代表,实现什么自我成长
    多模态训练如何平衡不同模态
    Java-调用R语言和调用Python(前后端展示)
    vued中图片路径与主机路径相关联,例如img:‘http://127.0.0.1:8000/media/data/els.jpg‘
    IO流之File类
    最全2000道Java后端面试题,从基础到进阶
    电脑清理c盘怎么清理全教程,教你彻底清理所有垃圾
    数据结构之链表
  • 原文地址:https://blog.csdn.net/weixin_45867071/article/details/126767954
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号