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

Example:

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7
  6.  
  7. There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public int sumOfLeftLeaves(TreeNode root) {
  12. int[] total = new int[];
  13. helper(root, false, total);
  14. return total[];
  15. }
  16.  
  17. void helper(TreeNode root, boolean isLeft, int[] total) {
  18. if (root == null) return;
  19. if (root.left == null && root.right == null) {
  20. if (isLeft) {
  21. total[] += root.val;
  22. }
  23. return;
  24. }
  25.  
  26. helper(root.left, true, total);
  27. helper(root.right, false, total);
  28.  
  29. }
  30. }

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. Sum of Left Leaves Easy Find the sum of all left leaves in a given binary tree. Example: 3 / \ ...

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

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

  4. [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 ...

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

  6. LeetCode Sum of Left Leaves

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

  7. 404. Sum of Left Leaves

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

  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. struts2基础篇(1)

    一.Struts2简介Struts2以WebWork优秀的设计思想为核心,吸收了Struts1的部分优点,建立了一个基于WebWork和Struts1的MVC框架. 二.搭建Struts2开发环境2. ...

  2. git alias和gitconfig配置

    [alias] st = status -sb co = checkout br = branch mg = merge ci = commit ds = diff --staged dt = dif ...

  3. 好看的CSS按钮

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  4. java web项目实现文件下载

    现在项目里面有个需求,需要把系统产生的日志文件给下载到本地先获取所有的日志文件列表,显示到界面,选择一个日志文件,把文件名传到后台: File file = new File(path);// pat ...

  5. Criteria 和 DetachedCriteria的区别与使用

    Criteria 和 DetachedCriteria 的主要区别在于创建的形式不一样, Criteria 是在线的,所以它是由 Hibernate Session 进行创建的:而 DetachedC ...

  6. Memcached存储命令 - set

    Memcached set 命令用于将 value(数据值) 存储在指定的 key(键) 中. 如果set的key已经存在,该命令可以更新该key所对应的原来的数据,也就是实现更新的作用. set 命 ...

  7. 时间处理工具类DateUtils

    public class DateUtils {         public static final String                            SHORT_DATE    ...

  8. 2015年12月01日 GitHub入门学习(二)手把手教你Git安装

    序:Mac与Linux中,Mac都预装了Git,各版本的Linux也都提供了Git的软件包.下面手把手教你Windows下的安装. 一.Git Windows GUI 下载地址 msysgit htt ...

  9. 2015年11月25 Java基础系列(二)Thread Runnable线程初级讲解

    序,线程是比进程小的进程,非常广泛的被使用. 一.继承Thread实现线程操作 1.注意setDaemon(boolean)方法,参数为true时为守护线程,参数为false时为用户线程. 守护线程的 ...

  10. 收到的电邮附件为Winmail.dat?

    以下信息来源于微软帮助中心:您收到电子邮件,其中包含一个 winmail.dat 的附件.电子邮件被人使用的 Microsoft Outlook 发送给您.该邮件的格式是丰富文本格式 (RTF). 原 ...