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;
    }
}
相关推荐
zc.z2 小时前
JAVA实现:纯PCM格式音频转换成BASE64
java·音视频·pcm
mask哥2 小时前
力扣算法java实现汇总整理(上)
java·算法·leetcode
Aaswk3 小时前
Java Lambda 表达式与流处理
java·开发语言·python
是宇写的啊3 小时前
Spring AOP
java·spring
万邦科技Lafite3 小时前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台
Mr_pyx5 小时前
Spring AI 入门教程:Java开发者的AI应用捷径
java·人工智能·spring
Zephyr_05 小时前
Leedcode算法题
java·算法
流年如夢6 小时前
栈和列队(LeetCode)
数据结构·算法·leetcode·链表·职场和发展
苍煜6 小时前
Java开发IO零基础吃透:BIO、NIO、同步异步、阻塞非阻塞
java·python·nio
折哥的程序人生 · 物流技术专研6 小时前
Java面试85题图解版(一):基础核心篇
java·开发语言·后端·面试