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;
    }
}
相关推荐
sharemylove7 小时前
Java集合框架全面详解
java·开发语言·经验分享·笔记·学习
FYKJ_20108 小时前
springboot众创项目团队管理系统94666-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·spark·课程设计
摇滚侠9 小时前
《On Java 中文版 基础卷》阅读笔记 内部类 20
java·笔记·python
FYKJ_201012 小时前
springboot贫困生认定管理系统59009-计算机毕业设计、课程设计
java·javascript·vue.js·spring boot·后端·django·课程设计
考虑考虑12 小时前
JDK27中的结构化并发
java·后端·java ee
旖旎夜光14 小时前
LeetCode 493: 翻转对 (归并排序) —— 题解
c++·笔记·学习·算法·leetcode·排序算法
吃我一窝jio14 小时前
Java IO流核心分类与应用
java·开发语言·笔记
这个昵称不容易15 小时前
租房管理系统设计与实现:16 张表、185 个接口的 SpringBoot 前后台项目
java·springboot·毕设
nice_lcj52017 小时前
Spring 事务:声明式事务怎么生效
java·spring·面试·事务·transactional·传播机制
沐知全栈开发17 小时前
基于Flask的班级信息与班务管理系统设计与实现-毕业设计
java·flask·课程设计