404. Sum of Left Leaves

Easy

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.
package leetcode.easy;

/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
public class SumOfLeftLeaves {
public int sumOfLeftLeaves(TreeNode root) {
if (null == root) {
return 0;
}
if (root.left != null && root.left.left == null && root.left.right == null) {
return root.left.val + sumOfLeftLeaves(root.right);
} else {
return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
}
} @org.junit.Test
public void test() {
TreeNode tn11 = new TreeNode(3);
TreeNode tn21 = new TreeNode(9);
TreeNode tn22 = new TreeNode(20);
TreeNode tn33 = new TreeNode(15);
TreeNode tn34 = new TreeNode(7);
tn11.left = tn21;
tn11.right = tn22;
tn21.left = null;
tn21.right = null;
tn22.left = tn33;
tn22.right = tn34;
tn33.left = null;
tn33.right = null;
tn34.left = null;
tn34.right = null;
System.out.print(sumOfLeftLeaves(tn11));
}
}

LeetCode_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. LeetCode 404. 左叶子之和(Sum of Left Leaves)

    404. 左叶子之和 404. Sum of Left Leaves LeetCode404. Sum of Left Leaves 题目描述 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 ...

  3. [LeetCode] 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. 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 ...

  5. LeetCode Sum of Left Leaves

    原题链接在这里:https://leetcode.com/problems/sum-of-left-leaves/ 题目: Find the sum of all left leaves in a g ...

  6. 404. Sum of Left Leaves

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

  7. 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. 16. leetcode 404. Sum of Left Leaves

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

  9. 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 ...

随机推荐

  1. OAuth 2.0攻击

    参考文章:https://www.yuque.com/pmiaowu/web_security_1/oauth 作者:PHPoop 关于OAuth2.0协议的授权流程可以参考下面的流程图: 1.Cli ...

  2. AtCoder Grand Contest 004题解

    传送门 \(A\) 咕咕 int a,b,c; int main(){ scanf("%d%d%d",&a,&b,&c); if((a&1^1)|( ...

  3. vuex(用了vue就上了一条不归路的贼船)

    一.Vuex是干什么用的? 它是用于对复杂应用进行状态管理用的(官方说法是它是一种状态管理模式). “杀鸡不用宰牛刀”.对于简单的项目,根本用不着Vuex这把“宰牛刀”.那简单的项目用什么呢?用Vue ...

  4. 性能测试学习第十天-----性能案例分析之CPU消耗过高&响应时间较长

    一.现象  /pinter/case/cpu?type=1   使用google的gjson.tojson性能较差    type=2 使用性能好的阿里巴巴的fastjson库 压测过程中,发现应用服 ...

  5. Kafka(一) —— 基本概念及使用

    一.安装&启动 安装Kafka(使用内置Zookeeper) 在Kafka官网下载安装包kafka_2.11-1.0.0.tgz #### 解压 tar zxvf kafka_2.11-1.0 ...

  6. Chapter One

    spring-boot-starter-parent spring-boot-starter-parent是一个特殊的Starter,提供了Maven的默认配置,同时还提供了dependency-ma ...

  7. [转]MATLAB 2018b 中文版下载与安装教程

    MATLAB R2018b_win64.iso 压缩包一个 MATLAB R2018b Win64 Crack 压缩包一个 一.下载与安装   1 下载中文原程序安装包和破解文件,链接: https: ...

  8. 高通平台打开 dynamic debug方法【学习笔记】

    一.首先在内核的配置文件添加如下的配置: CONFIG_DEBUG_FS=y CONFIG_DYNAMIC_DEBUG=y 二.找到自己平台对应的 device/qcom/xxxx/init.targ ...

  9. hibernate如何配置自动生成表

    hibernate自动生成表有两种方法: 1.直接写代码,通过方法来创建数据库表. 2.通过 hibernate.cfg.xml配置标签来创建数据表. 下面依次实现: 1.直接写代码,通过方法来创建数 ...

  10. 字节码(.class)文件的加载过程

    类加载 在Java代码中,类型的加载.连接与初始化过程都是在程序运行期间完成的. 类型可以是Class,Interface, 枚举等. Java虚拟机与程序的生命周期 在如下几种情况下,Java虚拟机 ...