[LeetCode] 404.Sum of Left Leaves

【笔记整理】轻量级神经网络 MobileNetV3 阅读详情

[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 left leaves in the binary tree, with values 9 and 15 respectively. Return 24.

解题思路

这道题的要点是怎样找到要加的数对应的节点,搞清楚树的结构以及特点就能知道,左叶节点需要满足的条件是:1.非空;2.无子节点。想到这两个条件以后利用树中常用的递归就能解决这个问题。

实验代码

/**
 * 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 sum = 0;
    void preorder(TreeNode* root) {
        if (root == NULL)
            return;
        TreeNode* T = root->left;
        if (T != NULL && T->left == NULL && T->right == NULL)
            sum += T->val;
        preorder(root->left);
        preorder(root->right);
    }
    int sumOfLeftLeaves(TreeNode* root) {
        preorder(root);
        return sum;
    }
};
YOLOV5 自动刷图脚本实战(十五)之如何找到正确的门 DXF是一个2D游戏,我们所看到的人物移动,其实是后面的背景在移动,我们所看到的画面,不过是一个完整背景中的一个镜头视角而已想要正确找到门的坐标,我们需要对不同的地图中的每个房间做一些配置信息。 阅读详情

相关推荐

基于51单片机的通讯聊天系统

通过编写51单片机通讯聊天程序,掌握51单片机的各个模块的知识点和运用C语言编写51单片机的能力。用51单片机与口袋开发板设计实现一个单片机与电脑串口助手实现通讯功能的程序。程序的主要功能如下: 1 OLED初始化显示Welcome to Bluetooth communication! 字符串。 2 程序开始时处于接收信息模式, 电脑串口助手发送信息时,将从初始化字符第一个开始显示,电脑串口助手发送信息后,蜂鸣器响铃2秒左右。 3 每次同时按下左右键时,可以轮流切换接收/发送信息模式 4 在发送信息模式时

m0_47419053的博客 3638

leetcode 404. Sum of Left Leaves

Python 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 left leaves in

accumulate_zhang的博客 812

手撕代码系列:Conv2D

在实际的神经网络中,每个卷积核都会对应一个偏置值。这意味着如果网络有多个卷积核,每个卷积核的输出都可以加上对应的偏置。

qq_55794606的博客 910

[leetcode]404. Sum of Left Leaves

[leetcode]404. Sum of Left Leaves Analysis everyday is the first day of your rest life—— [间歇性迷茫~] Find the sum of all left leaves in a given binary tree. 计算二叉树的所有左叶子节点的和,递归遍历。 Implement ...

weixin_32135877的博客 169

MIPI信号的分析--结合示波器实际测试波形

如果排查的思路对你有帮助,请记住 消雨匆匆 or 大吊工。码字和排查很累,仅此而已 ,硬件不是抄电路,拿烙铁,懂点软件,细致分析,找问题和破案搜集线索是一样的。 部分内容来自于此博客的大神总结: http://www.elecfans.com/news/wangluo/20170323499614.html http://www.elecfans.com/yuanqijian/jieko...

消雨匆匆 4万+

华为校招&实习&留学生机试真题目录+算法分类+在线OJ+通过率+视频讲解

全网最新最全华为校招&实习&留学生机试真题题库

m0_73659489的博客 6872

LeetCode C++ 404. Sum of Left Leaves 【Tree】

404. Sum of Left Leaves 【easy】 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...

memcpy0的博客 528

FPGA/IC 秋招笔试/面试题总结

文章目录一、FPGA内部资源1. 什么是FPGA2.FPGA内部资源二、同步时钟、同步/异步电路1.同步时钟2.同步/异步电路三、同步复位/异步复位1.同步复位2. 异步复位3. 异步复位同步释放四、同步FIFO/异步FIFO1.同步FIFO2.异步FIFO五、FIFO最小深度计算1.FIFO最小深度2.示例分析六、逻辑电平1.TTL电平标准2.LVTTL电平标准3.CMOS电平标准4.LVCOMS电平标准5.RS2326.RS4857.ECL、PECL、LVPECL8.LVDS9.CML10.GTL11

Crazzy_M的博客 4万+

[leetcode] 404. Sum of Left Leaves

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

Learning from the mistakes 164

Android日志实战——Tombstones日志分析(七)

Tombstones 一般是由 Dalvik 错误、状态监视调试器、C 层代码以及 libc 的一些问题导致的。当系统发生 Tombstone 的时候,内核(kernel)首先会上报一个严重的警告信号(signal),上层接收到之后,进程的调试工具会把进程中当时的调用栈现场保存起来,并在系统创建了 data/tombstones 目录后把异常时的进程信息写在此目录里面,开发者需要通过调用栈来分析整个调用流程来找出出问题的点。Tombstone原因分析pid 与 tid 相同,问题出在主线程。

小旭的专栏 2522

开源项目-智能算法中台管理系统

智能算法中台管理系统,系统主要有样本中心,算法中心,模型中心等模块

u010479989的博客 3001

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

【LeetCode】404.Sum of Left Leaves(Easy)解题报告题目地址:https://leetcode.com/problems/sum-of-left-leaves/description/ 题目描述:   Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9

郝春雨的博客 293

AIGC算法工程师面试八股文深度解析与进阶指南:揭秘Diffusion Models核心原理与实战技巧

总结来说,Cosine Noise Scheduler 的核心优势在于它能够更巧妙地平衡信号和噪声的比例,在扩散过程的早期阶段更长时间地保留原始图像的细节信息,从而为去噪模型提供一个更友好的学习环境,最终带来更优异的生成质量。🚀

FJN110的博客 346

LeetCode //C - 404. Sum of Left Leaves

【代码】LeetCode //C - 404. Sum of Left Leaves。

Made in Code 1014

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

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 left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 来源:力扣(LeetCode)...

中华小当家的博客 188

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

LeetCode 404 sum of left leaves

Find the sum of all left leaves in a given binary tree think about this, when we reached leaf, how are we gonna to know if it is left leaf or not? of course we can modify the iterate version of preorder traverse, it’s very simple. but how are we gonna do i

Tech in Pieces 320

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

LeetCode 404 Sum of Left Leaves

Problem: Find the sum of all left leaves in a given binary tree. Summary: 求左子叶之和。 Analysis: 1. 求左子叶之和,即需要遍历整棵二叉树,常规的方法则为递归。首先我想到的是调用子函数,在子函数中递归,每递归至一个新的节点,首先传入bool类型标记是根节点的左子树还是右子树。若为左子树,且为叶...

dilian4850的博客 90

[LeetCode]404. Sum of Left Leaves

[LeetCode]404. Sum of Left Leaves题目描述思路BFS和DFS均可 BFS较为简单 DFS需要考虑判断节点是否为左子节点的问题,参考了答案,在函数里添加了入参 isleft代码#include<iostream> #include<queue> using namespace std;struct TreeNode { int val; TreeNo

Lcharon的博客 604

leetcode404 sum of Left Leaves

求一个二叉树的所有左节点为叶子节点的和。 算法分析: (1)一个节点,当存在左子树的时候,进入计算,判断该节点是否为叶子节点。是的话,进行计算。当存在右子树的时候,判断该节点的右子树是否存在左子树,存在,则进行上述的计算。 (2)对每个节点都要进行上述的操作。 为了累计计算结果,我们用引用来保存计算结果。 代码如下: #include using namespace std; struct ...

weixin_41157881的博客 133

[LeetCode] 404. Sum of Left Leaves

原题链接:https://leetcode.com/problems/sum-of-left-leaves/ 1. 题目介绍 Find the sum of all left leaves in a given binary tree. 给出一个二叉树,计算所有左叶子节点的val值之和。 Example: There are two left leaves in the binary tree,...

Ber03的博客 231

【leetcode】404. Sum of Left Leaves【E】

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

sscssz的博客 1400

LeetCode 404. Sum of Left Leaves 解题报告

LeetCode 404. Sum of Left Leaves 解题报告

骆小坑的填坑之旅 2844

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 left leaves in the binary tree, with values 9 and 15 respectively. Return 24....

李歇特冯-兹拜因巴哈的博客 165

LeetCode笔记:404. Sum of Left Leaves

计算一个二叉树所有左叶子节点值之和

Minecraft 3417
上一篇: [LeetCode] 405.Convert a Number to Hexadecimal
下一篇: [LeetCode] 434.Number of Segments in a String
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值