Find the sum of all left leaves in a given binary tree.

Example:

    3
/ \
9 20
/ \
15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. Solution 1:
BFS
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
int res = 0;
if (root == null) {
return res;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
if (cur.left != null) {
if (cur.left.left == null && cur.left.right == null) {
res += cur.left.val;
} else {
queue.add(cur.left);
}
}
if (cur.right != null) {
queue.add(cur.right);
}
}
return res;
}
}

Solution 2:
DFS
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) {
return 0;
}
int res = 0;
if (root.left != null) {
if (root.left.left == null && root.left.right == null) {
res += root.left.val;
} else {
res += sumOfLeftLeaves(root.left);
}
}
res += sumOfLeftLeaves(root.right);
return res;
}
}

[LC] 404. Sum of Left Leaves的更多相关文章

  1. 【Leetcode】404. Sum of Left Leaves

    404. Sum of Left Leaves [题目]中文版  英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...

  2. 404. Sum of Left Leaves 左叶子之和

    [抄题]: Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are ...

  3. LeetCode - 404. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  4. 404. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. 左树的值(9+15=24) /** * Definition for a binary ...

  5. 16. leetcode 404. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. Example:     3    / \   9  20     /  \    15 ...

  6. LeetCode 404. Sum of Left Leaves (左子叶之和)

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  7. [LeetCode&Python] Problem 404. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  8. LeetCode 404. Sum of Left Leaves (C++)

    题目: Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are t ...

  9. 【LeetCode】404. Sum of Left Leaves 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...

随机推荐

  1. Python中Opencv和PIL.Image读取图片的差异对比

    近日,在进行深度学习进行推理的时候,发现不管怎么样都得不出正确的结果,再仔细和正确的代码进行对比了后发现原来是Python中不同的库读取的图片数组是有差异的. image = np.array(Ima ...

  2. nginx如何一个域名多个端口?

    方法一 写三个 listen server { listen 80; listen 81; listen 82; server_name www.sifou.com; ... 方法二 写三个serve ...

  3. 程序员用 Python 扒出 B 站那些“惊为天人”的UP主!

    ​ 前言 ! 近期B站的跨年晚会因其独特的创意席卷各大视频网站,给公司带来了极大的正面影响,股价也同时大涨,想必大家都在后悔没有早点买B站的股票: ​ 然而今天我们要讨论的不是B站的跨年晚会,而是B站 ...

  4. jq切换选择项

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. sp__helptable, sp__help******* help 存储过程

    from:  https://sqldbaknowledgeshare.wordpress.com/2014/11/27/general-help-procedures/ GENERAL HELP P ...

  6. 云托管,边缘物理计算&托管物理计算,你所需要了解的……

    随着业务发展,传统数据中心建设复杂性越来越高,基建的管理.设备的繁杂.人力成本的提升,是否让你的运维成本越来越高?企业生产效率却越来越低? 业务快速发展,设备采购周期冗长,大量采购造成CAPEX过重, ...

  7. Java之线程通信的方法

    /** * 线程通信的例子:使用两个线程打印 1-100.线程1, 线程2 交替打印 * * 涉及到的三个方法: * wait():一旦执行此方法,当前线程就进入阻塞状态,并释放同步监视器. * no ...

  8. Linux系统如何记录时间

    1.内核在开机启动的时候会读取RTC硬件获取一个时间作为初始基准时间,这个基准时间对应一个jiiffies值(这个基准时间换算成jiffies值的方法是:用这个时间减去1970-01-01  00:0 ...

  9. linux 中的.so和.a文件

    Linux下的.so是基于Linux下的动态链接,其功能和作用类似与windows下.dll文件. 下面是关于.so的介绍: 一.引言 通常情况下,对函数库的链接是放在编译时期(compile tim ...

  10. 五、Shell脚本高级编程实战第五部

    一.条件表达式 在bash的各种流程控制结构中通常要进行各种测试,然后根据测试结果执行不同的操作.有时也和if结合,让我们方便判断. test: 1)判断文件是否存在:test -f  file 2) ...