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
    }
};
相关推荐
橘子汽水1681 小时前
Leetcode 200,994岛屿数量,腐烂的橘子
数据结构·算法·leetcode
kiracrimson1 小时前
【无标题】
数据结构
Tisfy3 小时前
LeetCode 3871.统计范围内的逗号 II:每次算一个逗号
数学·算法·leetcode·题解
zcmodeltech3 小时前
冷链物流沙盘模型控制系统设计——基于STM32与Modbus RTU的冷藏车、冷库全流程动态展示方案
数据结构·stm32·单片机·嵌入式硬件·物联网·能源
find1star3 小时前
LeetCode 54:螺旋矩阵——用四个边界模拟矩阵收缩
java·算法·leetcode·边缘计算·学习方法
吞下星星的少年·-·4 小时前
LeetCode 热题 100 两数之和(哈希表)
算法·leetcode·哈希算法
Shan12054 小时前
经典算法题示例与详解:飞地的数量(二)
java·数据结构·算法
土司大王4 小时前
LeetCode hot100——全排列
java·算法·leetcode
203号居民5 小时前
LeetCode hot 100 — 138. 随机链表的复制
算法·leetcode·链表
土司大王6 小时前
LeetCode hot100——实现 Trie (前缀树)
java·算法·leetcode