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
    }
};
相关推荐
尋有緣13 分钟前
力扣1355-活动参与者
大数据·数据库·leetcode·oracle·数据库开发
kaikaile19951 小时前
基于拥挤距离的多目标粒子群优化算法(MO-PSO-CD)详解
数据结构·算法
不忘不弃1 小时前
求两组数的平均值
数据结构·算法
leaves falling1 小时前
迭代实现 斐波那契数列
数据结构·算法
Morwit1 小时前
*【力扣hot100】 647. 回文子串
c++·算法·leetcode
DonnyCoy1 小时前
Android性能之数据结构
数据结构
天赐学c语言2 小时前
1.7 - 删除排序链表中的重要元素II && 哈希冲突常用解决冲突方法
数据结构·c++·链表·哈希算法·leecode
菜鸟233号2 小时前
力扣96 不同的二叉搜索树 java实现
java·数据结构·算法·leetcode
千金裘换酒3 小时前
Leetcode 有效括号 栈
算法·leetcode·职场和发展
空空潍3 小时前
hot100-最小覆盖字串(day12)
数据结构·算法·leetcode