[LeetCode]404. Sum of Left Leaves

Pinocchio 安装教程|机器人学的必备库 安装pinocchio,不要安装pip3 install pinocchio! 阅读详情

[LeetCode]404. Sum of Left Leaves

题目描述

这里写图片描述

思路

BFS和DFS均可
BFS较为简单
DFS需要考虑判断节点是否为左子节点的问题,参考了答案,在函数里添加了入参 isleft

代码

#include<iostream>
#include<queue>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    /*BFS
    int sumOfLeftLeaves(TreeNode* root) {
        if (!root)
            return 0;
        queue<TreeNode*> q;
        int res = 0;
        q.push(root);
        while (!q.empty()){
            TreeNode* node = q.front();
            q.pop();

            if (node->left) {
                q.push(node->left);
                if (node->left->left == NULL && node->left->right == NULL)
                    res += node->left->val;
            }
            if (node->right)
                q.push(node->right);
        }
        return res;
    }
    */

    /*DFS*/
    int sumOfLeftLeaves(TreeNode* root, bool isleft = false) {
        if (!root)
            return 0;
        if (root->left == NULL && root->right == NULL)
            return isleft ? root->val : 0;
        return sumOfLeftLeaves(root->left, true) + sumOfLeftLeaves(root->right, false);
    }
};

int main() {
    TreeNode* r1 = new TreeNode(1);
    TreeNode* r2 = new TreeNode(1);
    TreeNode* r3 = new TreeNode(1);
    TreeNode* r4 = new TreeNode(1);

    r1->left = r2;
    r1->right = r3;
    r3->left = r4;

    Solution s;;
    cout << s.sumOfLeftLeaves(r1) << endl;
    system("pause");
}
详解AUTOSAR:AUTOSAR架构中的配置文件(拓展篇—7) 查看AUTOSAR代码包时会发现一些这样格式的文件:xxx_PBcfg.c、xxx_Lcfg.c和xxx_Cfg.h,本篇博文讲一讲这几个配置文件的作用和特点。 阅读详情

相关推荐

【YOLO 系列】基于YOLO V8的智能车载交通标志识别系统【python源码+Pyqt5界面+数据集+训练代码】

本文详细介绍了基于YOLO V8构建智能车载交通标志识别系统的完整流程。从环境搭建、数据集准备、模型训练到PyQt5图形界面开发,提供了Python源码和实战代码,帮助开发者快速实现高精度、实时的交通标志检测应用,提升驾驶辅助系统的开发效率。

wdx012345的博客 966

leetcode_404

二叉树左孩子和

yeal 863

llama.cpp部署大模型

llama.cpp如何部署大模型

Eddy的博客 979

[leetcode] 404 559 563

404. Sum of Left Leaves Find the sum of all left leaves in a given binary tree. 3 / \ 9 20 / \ 15 7 There are two left leaves in the binary tree, with v...

weixin_33918114的博客 137

CDO批量处理nc文件:5分钟搞定CMIP6数据双线性插值(附完整代码)

本文详细介绍了如何使用CDO工具链中的`remapbil`命令,快速实现CMIP6数据的批量双线性插值处理。通过环境配置、核心操作步骤、高级优化技巧及典型报错解决方案,帮助科研人员高效处理大规模nc文件,显著提升气象数据处理效率。

weixin_29267307的博客 292

MIT Cheetah Learning (一):State Estimate 从理论到实践:EKF融合IMU与编码器的状态估计全解析

本文深入解析了MIT Cheetah机器人中基于Extended Kalman Filter的状态估计技术,详细探讨了如何融合IMU与编码器数据以实现精准的State Estimate。通过四元数建模和传感器误差分析,揭示了EKF在复杂地形中的工程实践价值,为四足机器人的实时状态估计提供了完整解决方案。

weixin_30292843的博客 304

LeetCode笔记:404. Sum of Left Leaves

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

Minecraft 3417

Centos7.6下安装Zabbix6

一、

Overmaster博客 1906

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

a342500329a的专栏 264

C#实现Socket通信同时监听多客户端(附完整源码)

C#实现Socket通信同时监听多客户端(附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 427

基于 OpenCVSharp 的晶圆缺口自动检测与旋正方案

在半导体制造过程中,晶圆(Wafer)需要精确定位才能进行后续加工。每个晶圆边缘都有一个特定的缺口,这个缺口就像 "箭头" 一样,帮助机器确定晶圆的方向。我们的任务就是通过摄像头拍摄的图片,自动找到这个缺口的位置,计算出需要旋转多少角度才能让晶圆对准正确方向。简单来说,这个技术就像给晶圆 "找方向",确保它在生产线上始终保持正确的姿态。

品质生活,极致编程。 1211

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

从苏宁电器到卡巴斯基第02篇:我的本科时光(中)

怎么还分班了呢? 大概是在大二第二学期的时候吧,学院突然通知我们要分班。因为我们目前所有学生都是属于工商管理专业的,而从下个学期开始,就要分为会计、市场营销以及工商管理这三个方向。这还真是打了我一个措手不及,我都不知道还有分班这一回事。不过也没关系,我继续选择我的工商管理就好了。不过,在实际的选择结果上,像会计和工商管理这种学科,选的人自然是占了绝大多数,而选择市场营销的,大概也就是十

Gleam的专栏 9607

[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

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

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

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

[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

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

【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

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

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 //C - 404. Sum of Left Leaves

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

Made in Code 1014

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

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

[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 left leaves in the binary t

user_name_is_so_long的博客 252

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]383. Ransom Note
下一篇: [LeetCode]349. Intersection of Two Arrays
charon____
博客等级 码龄11年 3粉丝 202原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值