题目:

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++)的更多相关文章

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

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

  3. 16. leetcode 404. Sum of Left Leaves

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

  4. [leetcode]404. Sum of Left Leaves左叶子之和

    弄个flag记录是不是左节点就行 int res = 0; public int sumOfLeftLeaves(TreeNode root) { if (root==null) return res ...

  5. 【Leetcode】404. Sum of Left Leaves

    404. Sum of Left Leaves [题目]中文版  英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...

  6. 【LeetCode】404. Sum of Left Leaves 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...

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

  8. LeetCode之404. Sum of Left Leaves

    ------------------------------------------------------------------- 分两种情况: 1.当前节点拥有左孩子并且左孩子是叶子节点:左孩子 ...

  9. 404. Sum of Left Leaves

    Find the sum of all left leaves in a given binary tree. 左树的值(9+15=24) /** * Definition for a binary ...

随机推荐

  1. 升级优化关于日志生成logging封装TimedRotatingFileHandler

    1.变更升级:优化日志自定义输出到文件的level,以及文件夹生成用户自由控制 # coding=utf-8 import logging import time import os import l ...

  2. PAT乙级1027

    1027 打印沙漏 (20 分)   本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输 ...

  3. virtualbox虚拟机与物理机windows文件共享

    必须安装virtualbox的增强功能包(VBoxGuestAdditions) 1.打开Linux系统,选择 设备->安装增强增强功能 2.等待其自动安装,当出现press return to ...

  4. mysql索引和外键

    innodb外键: 1.CASCADE:从父表删除或更新会自动删除或更新子表中匹配的行 2.SET NULL:从父表删除或更新行,会设置子表中的外键列为NULL,但必须保证子表列没有指定NOT NUL ...

  5. 使用JQ实现统计剩余字数

    JQ实现统计文本框剩余字数 效果图: 代码如下,复制即可使用: <html lang="en"> <head> <meta charset=" ...

  6. Nginx服务器的平滑启动、平缓停止、平滑升级

    注:Nginx服务在运行时,会保持一个主进程(master process)和一个或多个工作进程(worker process). 每一个进程都会有一个PID进程号,可以通过向主进程的PID进程号发送 ...

  7. Web安全0003 - MySQL SQL注入 - union查询核心语法

    注:本文是学习网易Web安全进阶课的笔记,特此声明. 查库,select schema_name from information_schema.schemata; 查表,select table_n ...

  8. RNA-seq简单处理流程

    RNA_seq pipline RNA_seq pipline PeRl 2018年3月7日 首先说明一下我做RNA-seq处理流程的文件树格式: RNA-seq/ data/ GRCh38.gtf ...

  9. 20155236范晨歌_exp6信息搜集与漏洞扫描

    20155236范晨歌_exp6信息搜集与漏洞扫描 目录 实践目标 信息搜集 漏洞扫描 总结 实践目标 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口 ...

  10. 学习OpenCV——SVM

    学习OpenCV——SVM 学习SVM,首先通过http://zh.wikipedia.org/wiki/SVM, 再通过博客http://blog.csdn.net/yang_xian521/art ...