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
    }
};
相关推荐
每周报刊1 小时前
竞速座舱级视界:爱攻 AGP497UCZD 超宽 QDOLED 显示器解析
数据结构·游戏·计算机外设
鹿角片ljp2 小时前
LeetCode 141. 环形链表|从 HashSet 到快慢指针 O (1) 空间最优解
算法·leetcode·链表
小星星闪亮登场3 小时前
图论--最小生成树(内含二分图)
数据结构·算法·图论·迭代加深·图搜索算法
ZC跨境爬虫4 小时前
LeetCode 219. 存在重复元素 II(滑动窗口 + 哈希表详解)
算法·leetcode·散列表
Navigator_Z4 小时前
LeetCode //C - 1220. Count Vowels Permutation
c语言·算法·leetcode
2601_962201725 小时前
Java进阶,集合,Colllection,常见数据结构
java·数据结构·windows
feilieren5 小时前
leetcode - 389. 找不同
算法·leetcode
挽星安5 小时前
2026/8/29
数据结构·算法
positive_zpc5 小时前
进阶数据结构图——最短路径(二)
数据结构·算法·图论·最短路径
positive_zpc5 小时前
进阶数据结构图——最小生成树(一)
数据结构·算法·图论