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
    }
};
相关推荐
米粒14 小时前
力扣算法刷题 Day 27
算法·leetcode·职场和发展
Mr_Xuhhh5 小时前
LeetCode hot 100(C++版本)(上)
c++·leetcode·哈希算法
漫随流水5 小时前
c++编程:反转字符串(leetcode344)
数据结构·c++·算法
穿条秋裤到处跑7 小时前
每日一道leetcode(2026.03.31):字典序最小的生成字符串
算法·leetcode
Titan20249 小时前
map和set的封装学习笔记
数据结构·c++
Yupureki9 小时前
《算法竞赛从入门到国奖》算法基础:动态规划-路径dp
数据结构·c++·算法·动态规划
副露のmagic10 小时前
数组章节 leetcode 思路&实现
算法·leetcode·职场和发展
Frostnova丶10 小时前
LeetCode 3474. 字典序最小的生成字符串
算法·leetcode·职场和发展
AlenTech10 小时前
136. 只出现一次的数字 - 力扣(LeetCode)
leetcode
重庆小透明11 小时前
力扣刷题【3】相交链表
算法·leetcode·链表