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. 多线程实现的方式一继承Thread

    实现方法一:继承Thread类 package thread; /** * @function 多线程继承Thread类 * @author hj */ public class Threads ex ...

  2. bzoj1784: [Usaco2010 Jan]island

    现在居然出现一道题只有\(pascal\)题解没有\(C++\)题解的情况,小蒟蒻要打破它. 思维题:分类讨论 回归正题,此题十分考验思维,首先我们要考虑如何把不会走的地方给填上,使最后只用求一遍这个 ...

  3. CSS hack整理

    浏览器的兼容性一直是个头疼的问题,使用“欺骗”技术可使各个浏览器效果一致,花了些时间整理了各个浏览器的HACK,主要包括IE系列和最新版本的Chrome.Safari.Firefox. Opera,比 ...

  4. if语句:若边长小于等于0,则不进行计算;否则,计算正方形的面积

    #include<stdio.h>void main(){ float a; printf("Input the value:"); scanf("%f&qu ...

  5. 在golang中使用json

    jsoniter高性能json库 非常快,支持java和go marshal使用的一些坑 package main import ( "encoding/json" "f ...

  6. Centos 7 安装 dotnet 环境

    Centos 7 安装  dotnet 环境 下载官方 rpm yum 源 直接 yum install 安装rpm -Uvh https://packages.microsoft.com/confi ...

  7. word collocations中文版(信息检索)

    虽然说是大作业,也做了好几天,但是完全没有什么实际价值...就是把现有的东西东拼西凑一下,发现跑的特别慢还搞了个多核 写这篇blog纯属是我吃饱了没事干,记录一下装env的蛋疼 首先我们是在pytho ...

  8. 1090 Highest Price in Supply Chain (25)(25 分)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  9. ACwing : 798. 差分矩阵

    不得不说之前的差分我真的是掌握的不好.. 一维差分确实简单一看就会,但是学会了之后却并不能灵活的运用. 而二维的差分我甚至还琢磨了很长时间 懒得画图所以没有图..对于二维差分的定义,百度百科是这么说的 ...

  10. nodejs新工具-cypress和testcofe的崛起

    今天咨询一个自动化 工具问题,偶然间有人提起了这个可能以后会很火的工具,在此找到一篇很好的参考文章 记录并为以后做准备 cypress和testcofe https://www.jianshu.com ...