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
    }
};
相关推荐
圣保罗的大教堂2 分钟前
leetcode 3863. 将一个字符串排序的最小操作次数 中等
leetcode
Tisfy11 分钟前
LeetCode 3863.将一个字符串排序的最小操作次数:分类讨论(一次遍历)——简单易懂版
算法·leetcode·字符串·题解·分类讨论
0+11138 分钟前
算法 --递归
算法·leetcode
starzy19901 小时前
KafkaProducer 源码深度剖析:支撑高吞吐的数据结构设计
大数据·数据结构·kafka
y1su2 小时前
Leetcode 链表,树,回溯 模板
java·leetcode·链表
罗湖老棍子13 小时前
【例 1】Sightseeing Trip(信息学奥赛一本通- P1494)(洛谷-P10927)
数据结构·算法·图论·最短路·floyd·最小环
文丑颜不良啊13 小时前
LeetCode201~225题目解析
java·leetcode
郝亚军17 小时前
manifest 中 `sha256` 与 `signValue` 的作用
java·开发语言·数据结构
Nil20817 小时前
LeetCode 283移动零
数据结构·算法·leetcode
文丑颜不良啊18 小时前
LeetCode226~250题目解析
java·leetcode