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
    }
};
相关推荐
余额瞒着我当琳13 小时前
红黑树的实现--红黑树的概念、规则、效率、结构、旋转、查找、验证
数据结构·c++·算法
程序员南飞13 小时前
win系统安装minio
java·数据结构·算法
longlongzihan13 小时前
LeetCode 5. 最长回文子串:从暴力匹配到中心扩展的思维演进
c++·算法·leetcode
苦瓜打怪兽14 小时前
leetcode 热题100(11. 盛最多水的容器)
python·算法·leetcode
w_zero_one15 小时前
链表(4)
java·数据结构·算法
白山编程大哥16 小时前
C语言篇:语法进阶
c语言·数据结构·算法
Logic10117 小时前
C语言/数据结构动态规划题解:爬楼梯(含坏台阶)——避开障碍物的路径计数
c语言·数据结构·动态规划·时间复杂度·空间复杂度·算法题·爬楼梯
All for pursuit.18 小时前
【回溯-6】79.单词搜索
数据结构·c++·算法·leetcode
Navigator_Z18 小时前
LeetCode //C - 1281. Subtract the Product and Sum of Digits of an Integer
c语言·算法·leetcode
Benny_Tang18 小时前
AT_arc180_d [ARC180D] Division into 3 题解
数据结构·c++·算法