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.

题目标签:Tree
  这道题目给了我们一个二叉树,让我们找到所有左子叶之和。这里需要另外一个function - sumLeftLeaves 还要一个int res在两个functions外。对于sumLeftLeaves, 不需要返回,依次遍历每一个点,直到这个点是一个leaf node,就停止递归了。对于每一个点,有四种情况:
  1。这个点的两个children 都不是null, 这里需要检测left 是不是一个 leaf node, 如果是的话,加入它的值,继续递归left 和 right;
  2。这个点的left 是null, right 不是null, 那么递归right;
  3。这个点的left 不是null, right是null,需要检测left 是不是 left node, 是就加上它的值,继续递归left。
  4。这个点的两个children 都是null, 什么都不用写,不用递归,它自然停止。
 
 

Java Solution:

Runtime beats 77.28%

完成日期:07/05/2017

关键词:Tree

关键点:利用递归function 但是不需要返回,只遍历;

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

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 404. Sum of Left Leaves (左子叶之和)的更多相关文章

  1. [leetcode]404. Sum of Left Leaves左叶子之和

    弄个flag记录是不是左节点就行 int res = 0; public int sumOfLeftLeaves(TreeNode root) { if (root==null) return res ...

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

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

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

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

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

  7. 【Leetcode】404. Sum of Left Leaves

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

  8. LeetCode404Sum of Left Leaves左叶子之和

    计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9    20 / \ 15   7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 class Solution { pub ...

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

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

随机推荐

  1. Sql Server——基础

    前言: 在了解数据库之前,我们应该首先了解一下和数据库有关的知识,如:什么是数据,什么又是数据库等.  数据:描述事物的符号记录称为数据,它是数据库中存储的基本对象.  数据库(Datebase):数 ...

  2. SVN使用【介绍SVN、快速入门、解决冲突】

    什么是SVN SVN全称:Subversion,是一个开放源代码的版本控制系统 Svn是一种集中式文件版本管理系统.集中式代码管理的核心是服务器,所有开发者在开始新一天的工作之前必须从服务器获取代码, ...

  3. SpringMVC的数据格式化-注解驱动的属性格式化

    一.什么是注解驱动的属性格式化? --在bean的属性中设置,SpringMVC处理 方法参数绑定数据.模型数据输出时自动通过注解应用格式化的功能. 二.注解类型 1.DateTimeFormat @ ...

  4. 02-windows 安装以太坊 ethereum 客户端 (win7-64)-大叔思维

    以太坊(Ethereum)是一个运行智能合约的去中心化平台(Platform for Smart Contract),平台上的应用按程序设定运行,不存在停机.审查.欺诈.第三方人为干预的可能.以太坊平 ...

  5. 常用Linux命令、包括vi 、svn

    /etc/init.d/network restart//===========================================更新脚本cd /www/scripts更新站点./sta ...

  6. 系统学习java高并发系列三

    转载请注明原创出处,谢谢! 首先需要说说线程安全?关于线程安全一直在提,比如StringBuilder和StringBuffer有什么区别? 经常就会出现关于线程安全与线程非安全,可能一直在提自己没有 ...

  7. ElasticSearch 插件jdbc import(1)-----定时执行

    定时执行 参数schedule用来配置cron定时表达式 同时支持JSON数组的方式定义多个定时表达式: 例子如下:     "schedule" : "0 0-59 0 ...

  8. 51 nod 1624 取余最长路 思路:前缀和 + STL(set)二分查找

    题目: 写这题花了我一上午时间. 下面是本人(zhangjiuding)的思考过程: 首先想到的是三行,每一行一定要走到. 大概是这样一张图 每一行长度最少为1.即第一行(i -1) >= 1, ...

  9. 使用docker部署standalone cinder

    | 版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.如有问题,可以邮件:wangxu198709@gmail.com 背景 OpenSta ...

  10. Cow Uncle 学习了叉积的一点运用,叉积真的不错

    Cow Uncle Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitSta ...