计算给定二叉树的所有左叶子之和。

示例:

3

/ \

9    20

/ \

15   7

在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24

class Solution {
public:
int sum = 0;
int sumOfLeftLeaves(TreeNode* root)
{
if(root == NULL)
return 0;
if(root ->left == NULL && root ->right == NULL)
return 0;
if(root ->left != NULL && root ->left ->left == NULL && root ->left ->right == NULL)
{
sum += root ->left ->val;
}
if(root ->left)
{
sumOfLeftLeaves(root ->left);
}
if(root ->right)
{
sumOfLeftLeaves(root ->right);
}
return sum;
}
};

LeetCode404Sum of Left Leaves左叶子之和的更多相关文章

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

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

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

  3. [Swift]LeetCode404. 左叶子之和 | 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)

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

  5. 【leetcode 简单】 第九十四题 左叶子之和

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

  6. Java实现 LeetCode 404 左叶子之和

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

  7. [LeetCode]404. 左叶子之和(递归)、938. 二叉搜索树的范围和(递归)(BST)

    题目 404. 左叶子之和 如题 题解 类似树的遍历的递归 注意一定要是叶子结点 代码 class Solution { public int sumOfLeftLeaves(TreeNode roo ...

  8. 左叶子之和(sum-of-left-leaves)

    LeetCode题目--左叶子之和(sum-of-left-leaves) 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 ...

  9. 【LeetCode】404. 左叶子之和

    404. 左叶子之和 知识点:二叉树 题目描述 计算给定二叉树的所有左叶子之和.. 示例 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 解 ...

随机推荐

  1. axios调用接口

    axios调用接口 1. 按照axiosnpm install --save-dev axios2.在main.js 引入axios, 设置全局属性$http 指向axios main.js impo ...

  2. JAVA算法之简单排序

    冒泡排序: 在概念上是排序算法中最简单的,但是运行起来非常慢,冒泡排序遵循以下几个规则(假如我们现在要给一队打乱的足球队员排序): 比较两个队员 如果左边的队员比右边的高,则交换位置 向右移动一位,比 ...

  3. CSIC_716_20191119【常用模块的用法 subprocess、re、logging、防止自动测试、包的理论】

    subprocess模块 可以通过python代码给操作系统终端发送命令,并可以得到返回结果. import subprocess str = input('>>>请输入命令') # ...

  4. Bugs Integrated, Inc.

    Bugs Integrated, Inc. 给出一个\(n\times m\)的矩形网格图,给出其中K个障碍物的位置,求其中最多能摆的\(2\times 3\)的矩形的个数,\(n\leq 150,m ...

  5. Erlang学习记录:相关工具和文档

    在线工具和文档 网址 说明 OTP Reference Page Index 内置模块查询 Erlang/OTP Applications N Kernel Reference Manual 入门官方 ...

  6. 贪心+MST——cf1095F

    开始看错求最短路了.. 但是MST的思路和最短路也差不多 就是先不考虑特殊边,用最小点做一个生成树 然后加入特殊边,进行一次krus即可 #include<bits/stdc++.h> # ...

  7. 自定义Collection View布局

    转自answer-huang的博客 原文出自:Custom Collection View Layouts    UICollectionView在iOS6中第一次被介绍,也是UIKit视图类中的一颗 ...

  8. http://ajax.open-open.com/ ajax开源家园

    http://ajax.open-open.com/ 本站为广大OPEN爱好者搭建了一个OPEN家园,大家可以方便快捷地发布日志.上传照片,分享生活中的精彩瞬间:与好友一起玩转游戏,增加好友感情:创建 ...

  9. Delphi编写后台监控软件

    Delphi编写后台监控软件         文章来源:Delphi程序员之家     后台监控软件,为了达到隐蔽监控的目的,应该满足正常运行时,不显示在任务栏上,在按Ctrl+Alt+Del出现的任 ...

  10. 树链剖分(模板) 洛谷P3384

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...