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 分钟前
LeetCode反转链表
算法·leetcode·链表
圣保罗的大教堂2 小时前
leetcode 1161. 最大层内元素和 中等
leetcode
闲看云起2 小时前
LeetCode-day6:接雨水
算法·leetcode·职场和发展
黛色正浓3 小时前
leetCode-热题100-贪心合集(JavaScript)
javascript·算法·leetcode
一起努力啊~3 小时前
算法刷题--长度最小的子数组
开发语言·数据结构·算法·leetcode
小北方城市网3 小时前
第1课:架构设计核心认知|从0建立架构思维(架构系列入门课)
大数据·网络·数据结构·python·架构·数据库架构
leoufung3 小时前
LeetCode 221:Maximal Square 动态规划详解
算法·leetcode·动态规划
源代码•宸4 小时前
Leetcode—39. 组合总和【中等】
经验分享·算法·leetcode·golang·sort·slices
好易学·数据结构4 小时前
可视化图解算法77:零钱兑换(兑换零钱)
数据结构·算法·leetcode·动态规划·力扣·牛客网
AlenTech4 小时前
226. 翻转二叉树 - 力扣(LeetCode)
算法·leetcode·职场和发展