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
    }
};
相关推荐
tobias.b12 分钟前
408真题解析-2010-7-数据结构-无向连通图
数据结构·算法·图论·计算机考研·408真题解析
Cx330❀1 小时前
【优选算法必刷100题】第038题(位运算):消失的两个数字
开发语言·c++·算法·leetcode·面试
漫随流水1 小时前
leetcode回溯算法(93.复原IP地址)
数据结构·算法·leetcode·回溯算法
艾莉丝努力练剑1 小时前
【优选算法必刷100题】第021~22题(二分查找算法):山脉数组的峰顶索引、寻找峰值
数据结构·c++·算法·leetcode·stl
血小板要健康4 小时前
如何计算时间复杂度(上)
java·数据结构·算法
wWYy.4 小时前
详解哈希表
数据结构·算法·散列表
Lips6115 小时前
2026.1.25力扣刷题笔记
笔记·算法·leetcode
源代码•宸5 小时前
Leetcode—746. 使用最小花费爬楼梯【简单】
后端·算法·leetcode·职场和发展·golang·记忆化搜索·动规
WK100%5 小时前
二叉树经典OJ题
c语言·数据结构·经验分享·笔记·链表
沉默-_-5 小时前
力扣hot100-子串(C++)
c++·学习·算法·leetcode·子串