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
    }
};
相关推荐
8Qi812 分钟前
LeetCode 188 & 123:股票买卖问题(限制交易次数)—— 联合题解
算法·leetcode·职场和发展·动态规划
一只齐刘海的猫20 分钟前
【Leetcode】三数之和
数据结构·算法·leetcode
lightqjx20 分钟前
【算法】数据结构_扩展域并查集
数据结构·算法·并查集·扩展域并查集
San813_LDD23 分钟前
[量化]《多线程数据同步精讲:std::mutex 的底层原理与最佳实践》
c语言·数据结构
sheeta199830 分钟前
LeetCode 补拙笔记 日期:2026.06.07 题目:49. 字母异位词分组
笔记·算法·leetcode
Ricky_Theseus1 小时前
栈 & 队列 应用场景
数据结构·c++
ysu_03141 小时前
leetcode数据结构与算法5~7:链表双指针与二级指针
数据结构·学习·算法·leetcode·链表
小欣加油1 小时前
leetcode542 01矩阵
数据结构·c++·算法·leetcode·矩阵·bfs
Lucky_ldy1 小时前
数据结构从入门到精通:链表的分类
数据结构·链表
微风欲寻竹影1 小时前
Java数据结构——二叉树相关OJ题目详解
java·数据结构