LeetCode之Sum of Left Leaves
1、题目
Find the sum of all left leaves in a given binary tree.
Example:
3/ \9 20/ \15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
Subscribe to see which companies asked this question.
2、代码实现
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/
public class Solution {public int sumOfLeftLeaves(TreeNode head) {if (head == null) return 0;int left = 0, right = 0;if (head.left != null && head.left.left == null && head.left.right == null) {left = head.left.val;} else {left = sumOfLeftLeaves(head.left);}right = sumOfLeftLeaves(head.right);return left + right;}
}
LeetCode之Sum of Left Leaves相关推荐
- LeetCode 404. Sum of Left Leaves
题目: Find the sum of all left leaves in a given binary tree. Example: 3/ \9 20/ \15 7There are two le ...
- LeetCode 404. 左叶子之和(Sum of Left Leaves)
404. 左叶子之和 404. Sum of Left Leaves LeetCode404. Sum of Left Leaves 题目描述 计算给定二叉树的所有左叶子之和. 示例: 3/ \9 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 ...
- 【同113】LeetCode 129. Sum Root to Leaf Numbers
LeetCode 129. Sum Root to Leaf Numbers Solution1:我的答案 二叉树路径和问题,类似113 /*** Definition for a binary tr ...
- Java描述 LeetCode,404. Sum of Left Leaves 左叶子的节点总和
大家好,我是河海哥,专注于后端,如果可以的话,想做一名code designer而不是普通的coder,一起见证河海哥的成长,您的评论与赞是我的最大动力,如有错误还请不吝赐教,万分感谢.一起支持原创吧 ...
- C#LeetCode刷题之#404-左叶子之和(Sum of Left Leaves)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4084 访问. 计算给定二叉树的所有左叶子之和. 3 / ...
- LeetCode Path Sum III(前缀和)
问题: 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点). 二叉树 ...
- LeetCode Combination Sum IV(动态规划)
问题:给出一个数组nums和目标数target,问有多少组合形式 思路:用dp(i)表示目标数target的组合数.则有状态转移关系为dp(i)=sum(dp(i-nums[j])),其中i>= ...
- LeetCode Range Sum Query - Mutable(树状数组、线段树)
问题:给出一个整数数组,求出数组从索引i到j范围内元素的总和.update(i,val)将下标i的数值更新为val 思路:第一种方式是直接根据定义,计算总和时直接计算从i到j的和 第二种方式是使用树状 ...
最新文章
- 放弃 Notepad++,事实证明,还有 5 款更牛逼……
- 树的方向,风决定;人的方向,自己决定
- 开发笔记:掉落系统模块设计思路
- 进阶指南:如何编写可重用程序
- linux下进程监控6,Linux进程监控技术—精通软件性能测试与LoadRunner最佳实战(6)...
- Python3bytes转16进制字符(例如:b111111转\x8c\x8c\x8c\x8c\x8c\x8c)
- Excel数据分析实用小技巧【过坑】
- SpringBoot 工程目录 整合mybatis-mysql(注解类型)
- linux虚拟机模板部署模板,创建和部署基于 Linux 的虚拟机模板
- 查看iOS App的bundleId
- 向C语言之父—丹尼斯·里致敬
- 2020-06-16
- python画密度散点图_python散点图密度怎么计算
- R-S编码译码-缩短码(10,6)
- TCPC 可用于笔记本,平板等HOST端的解决方案
- 宏观经济学-大题资源整理
- ESP32串口通信 双机串口通信
- 交通运输相关机器学习/深度学习相关资源列表,关于交通运输场景的机器学习/深度学习的资源这里都有
- “九”答不可 | 量子通信如何做到安全保密?
- Android自定义View(一)
热门文章
- 比较两个字符串的相似度算法
- 基于单TCP连接的高吞吐模型设计
- 老刘在微软Ignite China大会上聊低代码
- ASP.NET Core 对Controller进行单元测试
- Goodbye 2020,Welcome 2021 | 沉淀 2021
- 用重构指导Clean Code(二):依恋情结和switch语句
- 几个超级实用但很少人知道的 VS 技巧[更新]
- 造轮子-AgileConfig一个基于.NetCore开发的轻量级配置中心
- 【壹刊】Azure AD(二)调用受Microsoft 标识平台保护的 ASP.NET Core Web API (上)
- 以Blog.Core的方式来打开Abp.vNext