弄个flag记录是不是左节点就行

int res = 0;
public int sumOfLeftLeaves(TreeNode root) {
if (root==null) return res;
leftSum(root,false);
return res;
}
private void leftSum(TreeNode root,boolean flag)
{
if (root.left==null&&root.right==null&&flag)
res+=root.val;
if (root.left!=null)
leftSum(root.left,true);
if (root.right!=null)
leftSum(root.right,false);
}

[leetcode]404. Sum 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. LeetCode404Sum of Left Leaves左叶子之和

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

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

  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 (C++)

    题目: Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are t ...

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

  7. 16. leetcode 404. Sum of Left Leaves

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

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

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

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

随机推荐

  1. macos brew zookeeper,安装后zookeeper启动失败?

    一.Zookeeper安装流程 执行如下安装命令: brew install zookeeper 执行截图如下: 安装后查看 zookeeper 安装信息(默认拉取最新版本) brew info zo ...

  2. moviepy音视频剪辑:视频剪辑基类VideoClip详解

    ☞ ░ 前往老猿Python博文目录 ░ 一.概述 在<moviepy音视频剪辑:moviepy中的剪辑基类Clip详解>和<moviepy音视频剪辑:moviepy中的剪辑基类Cl ...

  3. 关于moviepy打包报错AttributeError: module audio/video.fx.all has no attribute fadein、crop文章的纠错和抄袭

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 老猿前面有篇文章<moviepy应用pyin ...

  4. 由Java 15废弃偏向锁,谈谈Java Synchronized 的锁机制

    Java 15 废弃偏向锁 JDK 15已经在2020年9月15日发布,详情见 JDK 15 官方计划.其中有一项更新是废弃偏向锁,官方的详细说明在:JEP 374: Disable and Depr ...

  5. 你必须知道的MySQL知识点

    什么是索引 索引是帮助MySQL高效获取数据的排好序的数据结构 索引数据结构(掌握) 数据结构可视化 前置知识:树的高度越低查询效率越高 二叉树:不能自平衡,极端情况出现倾斜,查询效率和链表类似 红黑 ...

  6. 返回sourceString 中出现的第一个 searchString 的索引

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 手把手教你写DI_3_小白徒手支持 `Singleton` 和 `Scoped` 生命周期

    手把手教你写DI_3_小白徒手支持 Singleton 和 Scoped 生命周期 在上一节:手把手教你写DI_2_小白徒手撸构造函数注入 浑身绷带的小白同学:我们继续开展我们的工作,大家都知道 Si ...

  8. 【题解】NOI 系列题解总集

    每次做一道 NOI 系列的估计都很激动吧,对于我这种萌新来说( P1731 [NOI1999]生日蛋糕 练习剪枝技巧,关于剪枝,欢迎看我的垃圾无意义笔记 这道题是有一定难度的,需要运用各种高科技剪枝( ...

  9. BJOI2016 回转寿司

    题目链接 Description 给定一个长度为 \(N\) 的序列 \(a\),和一个区间 \([L, R]\). 求多少连续子序列的权值和在区间内,即满足 \(1 \le i \le j \le ...

  10. spark中map和mapPartitions算子的区别

    区别: 1.map是对rdd中每一个元素进行操作 2.mapPartitions是对rdd中每个partition的迭代器进行操作 mapPartitions优点: 1.若是普通map,比如一个par ...