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 two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
分析:
给定一颗二叉树,求左叶子节点的和。
重点在于如何判断左叶子节点,如果一个节点的left存在,且left的left和right都为空,那么我们就可以将这个节点的left->val记录下来。递归处理整颗树即可。
程序:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if(!root)
return ;
int sum = ;
if (root->left && !root->left->right && !root->left->left){
sum = root->left->val;
}
return sum + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
}
};
LeetCode 404. Sum of Left Leaves (C++)的更多相关文章
- 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 ...
- 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 ...
- 16. leetcode 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 ...
- [leetcode]404. Sum of Left Leaves左叶子之和
弄个flag记录是不是左节点就行 int res = 0; public int sumOfLeftLeaves(TreeNode root) { if (root==null) return res ...
- 【Leetcode】404. Sum of Left Leaves
404. Sum of Left Leaves [题目]中文版 英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...
- 【LeetCode】404. Sum of Left Leaves 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- [LeetCode&Python] Problem 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 ...
- LeetCode之404. Sum of Left Leaves
------------------------------------------------------------------- 分两种情况: 1.当前节点拥有左孩子并且左孩子是叶子节点:左孩子 ...
- 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. 左树的值(9+15=24) /** * Definition for a binary ...
随机推荐
- Entity Framework Code First 遭遇主键自动生成问题
4.0后就没有去跟踪后面的版本了.现在直接开始用5.0没想到在做User的GURD时就遭遇insert不进数据问题. ISet<User>.Add(user);_context.SaveC ...
- C库函数重定向问题
C库函数重定向用户能定义自己的C语言库函数,连接器在连接时自动使用这些新的功能函数.这个过程叫做重定向C语言库函数,如下图所示.举例来说,用户有一个I/O设备(如UART).本来库函数fputc()是 ...
- block本质探寻五之atuto类型局部实例对象
说明:阅读本文章,请参考之前的block文章加以理解: 一.栈区block分析 //代码 //ARC void test1() { { Person *per = [[Person alloc] in ...
- Go语言学习 总结一
1,定义main, package main 一个可独立执行的程序,(类似main方法) 2,import “fmt” fmt 实现格式化IO(输入/输出) (类似system.out.print() ...
- 重学Verilog(3)——参数化模块
1.parameter方法 首先有这样一个模块 module half_adder(co,sum,a,b); output co,sum; input a,b; ; ; and #and_delay ...
- 生死系列--WuJie
WuJie,高中和中专时期的同学,篮球队的队友. 在高三时认识的,我们隔壁班的,但仅限于认识,并未打过交道.高中毕业后考在同一所学校,同一个班,象棋下的很好,喜欢打扑克牌,在班上任团支部书记. 球队时 ...
- 20155210 潘滢昊2016-2017-2 《Java程序设计》第9周学习总结
20155210 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC驱动的四种类型(按操作方式分类的): JDBC-ODBC Bridge Driver ...
- @ModelAttribute三个作用:
@ModelAttribute具有如下三个作用: ①绑定请求参数到命令对象:放在功能处理方法的入参上时,用于将多个请求参数绑定到一个命令对象,从而简化绑定流程,而且自动暴露为模型数据用于视图页面展示时 ...
- elastic-job+zookeeper实现分布式定时任务调度的使用(springboot版本)
总体思路,要确认一个定时任务需要一个cron表达式+jobDetail: 现在要让实现定时任务的协调,则就让zookeeper,简单说就是需要3要素,zk对象+cron+jobDetail: 总的项目 ...
- 26-[Boostrap]-介绍与起步
1.Bootstrap的介绍 凡是使用过Bootstrap的开发者,都不在乎做这么两件事情:复制and粘贴.哈哈~,是的使用Bootstrap非常简单,但是在复制粘贴之前,需要先对Bootstrap的 ...