Java | Leetcode Java题解之第404题左叶子之和

题目:

题解:

java 复制代码
class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) {
            return 0;
        }

        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        int ans = 0;
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            if (node.left != null) {
                if (isLeafNode(node.left)) {
                    ans += node.left.val;
                } else {
                    queue.offer(node.left);
                }
            }
            if (node.right != null) {
                if (!isLeafNode(node.right)) {
                    queue.offer(node.right);
                }
            }
        }
        return ans;
    }

    public boolean isLeafNode(TreeNode node) {
        return node.left == null && node.right == null;
    }
}
相关推荐
代码飞天3 分钟前
wireshark的高级使用
android·java·wireshark
灰色小旋风14 分钟前
力扣21 合并两个有序链表(C++)
c++·leetcode·链表
gechunlian8826 分钟前
Spring Boot中的404错误:原因、影响及处理策略
java·spring boot·后端
岁岁种桃花儿36 分钟前
AI超级智能开发系列从入门到上天第四篇:AI应用方案设计
java·服务器·开发语言
王老师青少年编程40 分钟前
2026年3月GESP真题及题解(C++五级):有限不循环小数
c++·题解·真题·gesp·csp·五级·有限不循环小数
架构师沉默1 小时前
Java 终于有自己的 AI Agent 框架了?
java·后端·架构
程序员爱酸奶1 小时前
ThreadLocal内存泄漏深度解析
java
czlczl200209251 小时前
JVM创建对象过程
java·开发语言
一直都在5721 小时前
线程间的通信
java·jvm
老鼠只爱大米2 小时前
LeetCode经典算法面试题 #347:前 K 个高频元素(最小堆、桶排序、快速选择等多种实现方案详解)
算法·leetcode·堆排序·java面试题·桶排序·快速选择·topk