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 ...
随机推荐
- 史上最详细的codeblocks安装教程!!!
codeblocks下载地址:https://www.cnblogs.com/yinbiao/p/8489748.html 下面是codeblocks具体的安装教程:
- 【LeetCode43】 Multiply Strings
题目描述: 解题思路: java代码: public class LeetCode43 { public static void main(String[] args) { String num1=& ...
- uniapp开发踩坑记录
数组绑定class的问题 版本:v1.5.4 自定义了一个icon的组件,部分代码如下 <template> <text :class="[name, icon]" ...
- 人生苦短,我用python
星空不问赶路人,时光不负有心人,你可以脱变. 1.计算机的初步认识 2.解释器的安装 python2.7(2020年官方不在维护) python3.6 (官方推荐) 1.下载安装包 https://w ...
- 高德地图API(流程法)整理分析
[高德地图API(流程法)分析]: 前言:公司现在的网约车项目,使用的是高德地图,因为地图导航这一块的功能占比量比较大,为了方便大家对高德地图API的了解和学习使用,使用流程图把高德API分析整理了下 ...
- react fake double , bind click and dblclick on the same element
smartClick:function(id,name,waiter,e){ var desk = $$(e.currentTarget).data('raw'); if(this.lastClick ...
- 2017-2018-1 20155209 实现mypwd
2017-2018-1 20155209 实现mypwd 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd 首先使用 ...
- 洛谷 P1306 斐波那契公约数
洛谷 P1306 斐波那契公约数 题目描述 对于Fibonacci数列:1,1,2,3,5,8,13......大家应该很熟悉吧~~~但是现在有一个很“简单”问题:第n项和第m项的最大公约数是多少? ...
- 微信小程序:实现日历功能
一.功能描述 实现日历功能 二. 代码实现 1. index.wxml <view class='wrap'> <view> <view class='date-show ...
- hive streaming 使用的时候的一些心得
hive streaming 报错的解决方案: 1.把使用到hive streaming 的sql 分解,例如:select transform a,b,c,d using 'python cc.py ...