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. c++ 深度优先算法

    #include <iostream> using namespace std; #define VertexNum 9 /*定义顶点数*/ struct Node /*声明图形顶点结构* ...

  2. nginx基础知识小结

    配置文件讲解: #user nobody; #开启进程数 <= CPU数 worker_processes 1; #错误日志保存位置 #error_log logs/error.log; #er ...

  3. Map的6种遍历方法

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 探讨有几种遍历Map的方法其实意义并不大,网上的文章一般讲4种或5种的居多,重要的是知道遍历的内涵,从遍历 ...

  4. Java线程——线程之间的数据共享

      在 Java 传统线程机制中的共享数据方式,大致可以简单分两种情况: ➢ 多个线程行为一致,共同操作一个数据源.也就是每个线程执行的代码相同,可以使用同一个 Runnable 对象,这个 Runn ...

  5. 推荐Markdown编辑器——Inspire

    推荐Markdown编辑器--Inspire Inspire是一款非常好用的编辑器,支持Markdown语法,当然,Inspire还有一些自己的语法. 本文就是在这款编辑器下编写的. 风格 像Visu ...

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

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

  7. memset的常见用法

    头文件 <cstring> 描述 因为memset函数按照字节填充,所以一般memset只能用来填充char型数组 ------------------------------------ ...

  8. JavaWeb部分视频\2-12JSP,EL和JSTL

    JavaWeb知识结构图 第3节 EL介绍和运算符 && 第4节 EL获取域中存储的数据 ## EL表达式 1. 概念:Expression Language 表达式语言 2. 作用: ...

  9. Druid数据库连接池获取连接阻塞(转载)

    一. 背景        17年公司有个项目组在南京做项目的时候,开发框架用的是spring boot ,数据库连接池用的是druid,但老是遇到socket read timeout的错误,不得已放 ...

  10. day65-CSS选择器和样式优先级

    1. CSS CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). 2.CSS语法 每个 ...