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. Ring3层 UNICODE_STRING

    今天写驱动用到UNICODE_STRING,就在Ring3层抠了一些源代码,学习一下,不多说了上代码了 #pragma once #include <windows.h> #include ...

  2. springmvc05-Spring+Springmvc+Hibernate实现简单的用户管理系统

    1, 导入\spring-framework-3.2.4.RELEASE\libs\disk下所有包; hibernate-distribution-3.6.7.Final\lib\required下 ...

  3. StringBuffer的替换和反转和截取功能

    A:StringBuffer的替换功能 * public StringBuffer replace(int start,int end,String str): * 从start开始到end用str替 ...

  4. javascript 单元测试初入门

    1.使用mocha工具实现单元测试 ①首先准备node环境 ②安装mocha:npm install mocha 也可以进行全局安装 npm install global mocha ③安装断言库:n ...

  5. java中判断字符串是否为数字的方法的几种方法

    1.用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = 0; i < str.length(); i++){ ...

  6. 在 macOS High Sierra 10.13 搭建 PHP 开发环境

    2017 年 9 月 26 日,苹果公司正式发布了新一代 macOS,版本为 High Sierra (11.13). macOS High Sierra 预装了 Ruby(2.3.3).PHP(7. ...

  7. Maven仓库搜索jar包依赖网址

    可在该网站搜索jar包依赖 http://search.maven.org/

  8. 安装Vue2的devtools发生错误npm ERR! code EINTEGRITY npm ERR! sha1-HTFDXrRzR2Ew8Bv9zlMSCVgPsS0= integrity checksum failed when using sha1: wanted sha1-HTFDXrRzR2Ew8Bv9zlMSCVgPsS0= but got sha1-Z6BeTMF4nhAO6h5A

    1.github下载地址:https://github.com/vuejs/vue-devtools 2.下载好后进入vue-devtools-master工程  执行npm install ---- ...

  9. HDFS概述(4)————HDFS权限

    概述 Hadoop分布式文件系统(HDFS)的权限模型与POSIX模型的文件和目录权限模型一致.每个文件和目录与所有者和组相关联.该文件或目录将权限划分为所有者的权限,作为该组成员的其他用户的权限.以 ...

  10. webpack2使用ch2-entry和output简要说明

    1 entry打包入口 打包字符串和数组 const webpack = require('webpack'), path = require('path'); module.exports = { ...