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
    }
};
相关推荐
WBluuue4 分钟前
数据结构与算法:树上启发式合并
数据结构·c++·算法·启发式算法
不是光头 强18 分钟前
feign-list-param-crash-cpp
java·数据结构·list
x_xbx21 分钟前
LeetCode:20. 有效的括号
算法·leetcode·职场和发展
计算机安禾21 分钟前
【算法设计与分析】第40篇:空间数据结构:KD树与四叉树的查询分析
数据结构·算法
努力努力再努力wz25 分钟前
【C++高阶数据结构系列】:跳表 SkipList 详解:多层索引、随机晋升与C++ 完整实现(附跳表实现的源码)
开发语言·数据结构·数据库·c++·redis·缓存·skiplist
代码中介商44 分钟前
图论入门:从基础到遍历算法
数据结构·算法·图论
8Qi81 小时前
LeetCode 295:数据流的中位数(Median Finder)—— Java 题解 ✅
java·算法·leetcode·优先队列··中位数
飞天狗1111 小时前
2024第十五届蓝桥杯c/c++B组国赛题解
c语言·数据结构·c++·算法·蓝桥杯
练习时长一年3 小时前
LeetCode热题100(二叉树的最大路径和)
算法·leetcode·职场和发展
2401_872418789 小时前
算法入门:数据结构-堆
数据结构·算法