404、左叶子之和

题目链接:

递归

cpp 复制代码
class Solution {
public:    
    int sumOfLeftLeaves(TreeNode* root) {
        int ans = 0;
        if (!root) return 0;
        
        if (root->left) {
            if ((!root->left->left) && (!root->left->right)) {
                ans += root->left->val;
            }
        }
        
        ans += sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);//
        return ans;//这里一开始写成了return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right) QAQ
    }
};
相关推荐
YHHLAI13 小时前
LeetCode 1.两数之和 | 从暴力枚举到线性优化
算法·leetcode·职场和发展
sheeta199814 小时前
LeetCode 每日一题笔记 日期:2026.06.14 题目:2130. 链表最大孪生和
笔记·leetcode·链表
Tisfy14 小时前
LeetCode 3838.带权单词映射:求和、取模、拼接(附python一行版)
python·算法·leetcode·字符串·题解·模拟·取模
Y_Bk14 小时前
第十七届蓝桥杯C/C++A组省赛
c语言·数据结构·c++·算法·蓝桥杯
帅小伙―苏14 小时前
力扣76最小覆盖子串
算法·leetcode
RH23121115 小时前
2026.5.24 数据结构 KMP算法实现
数据结构·算法
想吃火锅100515 小时前
【leetcode】20.有效的括号js
linux·javascript·leetcode
Misnearch15 小时前
Leetcode热题100
算法·leetcode·职场和发展
悠仁さん15 小时前
数据结构 图(概念篇)
数据结构
带土115 小时前
1. 数据结构简单复习回顾(线性结构)
数据结构