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

示例:

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. sql (5) 左右连接

    左连接 SQL LEFT JOIN LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行.语法SELECT colu ...

  2. Android开发 string.xml资源添加参数

    挖坑:参考:https://www.cnblogs.com/leelugen/p/6685905.html

  3. Android开发 LiveData与MutableLiveData详解

    前言 LiveData与ViewMode是经常搭配在一起使用的,但是为了不太混乱,我还是拆分开来说明,此篇博客只讲解 LiveData 与 MutableLiveData的概念与使用方式(但是会涉及到 ...

  4. XYIXY.COM短网址在线生成,快速、稳定、永久有效,免费开放网址缩短API接口。

    在PHP中使用API 要在PHP程序中使用API,您必须通过file_get_contents或cURL发送GET请求:两者都是可靠的方法,您可以直接复制下面的代码. <?php /**** S ...

  5. 「STL」bitset正传

    前言 之前一些需要转二进制来解决的题目我看到很多大佬用了bitset. 然而我并不会这东西.看上去很高级的样子…… 改题改累了来学习一下233. 正文 一.bitset的构造 bitset有三种构造方 ...

  6. 计数dp+概率+大数——(抽屉问题解的个数)zoj3380

    难的地方在于计数dp..给定范围[1,n]的数去填m个位置,要求不能出现超过I个相同的数, 那就用dp[i][j]表示在阶段i,已经填了j个位置的可能解法,那么只要枚举i填的位置数k∈[0,min(j ...

  7. 使用SharpZipLib实现zip压缩

      使用国外开源加压解压库ICSharpCode.SharpZipLib实现加压,该库的官方网站为http://www.icsharpcode.net/OpenSource/SharpZipLib/D ...

  8. day 72 Django基础七之Ajax

    Django基础七之Ajax   本节目录 一 Ajax简介 二 Ajax使用 三 Ajax请求设置csrf_token 四 关于json 五 补充一个SweetAlert插件(了解) 六 同源策略与 ...

  9. 左神算法基础班4_1&2实现二叉树的先序、中序、后序遍历,包括递归方式和非递归

    Problem: 实现二叉树的先序.中序.后序遍历,包括递归方式和非递归方式 Solution: 切记递归规则: 先遍历根节点,然后是左孩子,右孩子, 根据不同的打印位置来确定中序.前序.后续遍历. ...

  10. 最最最详细的IDEA导入Eclipse项目

    很详细的IDEA导入Eclipse项目,配置tomcat并运行项目 1.把Eclipse项目复制一份,放到自己指定的位置 2.打开Idea,在进入工程前选择,inmport Project 注意事项: ...