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
    }
};
相关推荐
蛋蛋的就会顺顺的2 小时前
hot100——矩阵
java·数据结构·算法·leetcode·力扣
学代码的CJY3 小时前
从代码出发理解时间复杂度与空间复杂度
数据结构·算法
字节跳动的猫4 小时前
LikeShop 积分体系全链路改造:获取规则、订单抵扣与积分商品兑换二开
运维·数据结构
上位机妹子5 小时前
C 语言 数组删除指定元素(快慢指针法)
c语言·数据结构·算法
想吃火锅10056 小时前
【leetcode】238.除了自身以外数组的乘积js
javascript·算法·leetcode
C++ 老炮儿的技术栈7 小时前
有符号变量与无符号变量的区别
服务器·开发语言·前端·数据结构·c++·算法·c
Tisfy8 小时前
LeetCode 0020.有效的括号:栈匹配
算法·leetcode·字符串·题解·括号匹配
Navigator_Z8 小时前
LeetCode //C - 1276. Number of Burgers with No Waste of Ingredients
c语言·算法·leetcode
keyipatience8 小时前
跳表详细解析
开发语言·数据结构·c++·算法·跳表
想吃火锅10059 小时前
【leetcode】189.轮转数组js
开发语言·javascript·leetcode