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;
    }
}
相关推荐
卓怡学长1 分钟前
w181springboot机场乘客服务系统
java·数据库·spring boot·spring·intellij-idea
萧瑟余晖17 分钟前
Java深入解析篇三十九之集成测试
java·开发语言·集成测试
吴声子夜歌1 小时前
Java面试——数据结构(一)
java·数据结构·面试
罗超驿1 小时前
SpringBoot 快速入门
java·spring boot·后端
wabs6661 小时前
关于栈【力扣1047. 删除字符串中的所有相邻重复项的思考】
数据结构·c++·算法·leetcode··代码随想录
疯狂打码的少年2 小时前
【数据结构】选择类排序:简单选择与堆排序
java·数据结构·笔记·算法
吴声子夜歌2 小时前
Java面试——基础
java·开发语言·面试
智码看视界2 小时前
Day 56:AI辅助开发全面提效:Copilot + Cursor的Java开发
java·单元测试·copilot·cursor·后端开发·代码审查·ai辅助开发
asdfg12589632 小时前
一个例子理解Java中的函数式接口
java·函数式接口
toolsmith3 小时前
一个商业软件的License校验,居然把验签的证书放在了License文件里
java