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;
    }
}
相关推荐
YGGP1 天前
【Golang】LeetCode 64. 最小路径和
算法·leetcode
.鸣1 天前
set和map
java·学习
ha_lydms1 天前
5、Spark函数_s/t
java·大数据·python·spark·数据处理·maxcompute·spark 函数
黄河滴滴1 天前
java系统变卡变慢的原因是什么?从oom的角度分析
java·开发语言
侠客行03171 天前
Mybatis二级缓存实现详解
java·mybatis·源码阅读
LYFlied1 天前
【每日算法】LeetCode 1143. 最长公共子序列
前端·算法·leetcode·职场和发展·动态规划
老华带你飞1 天前
农产品销售管理|基于java + vue农产品销售管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
Edward111111111 天前
tomcat_servlet
java·servlet·tomcat
短剑重铸之日1 天前
SpringBoot声明式事务的源码解析
java·后端·spring·springboot
李白的粉1 天前
基于springboot的银行客户管理系统(全套)
java·spring boot·毕业设计·课程设计·源代码·银行客户管理系统