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
    }
};
相关推荐
逻极几秒前
Redis 从入门到精通:缓存设计与实战
数据结构·redis·缓存·哨兵集群
ʚ希希ɞ ྀ2 分钟前
全排列 --- 回溯
算法·leetcode·深度优先
玉树临风ives2 分钟前
atcoder ABC 460 题解
数据结构·c++·算法
少司府4 分钟前
C++进阶:二叉搜索树
开发语言·数据结构·c++·二叉树·stl·二叉搜索树·tree
8Qi89 分钟前
LeetCode 124. 二叉树中的最大路径和(Hard)
算法·leetcode·二叉树·递归
不会就选b10 分钟前
数据结构之单链表
数据结构
And_Ii13 分钟前
LeetCode 1. 两数之和 python
数据结构·算法·leetcode
cpp_250123 分钟前
P10377 [GESP202403 六级] 好斗的牛
数据结构·c++·算法·题解·洛谷·gesp六级
邪修king23 分钟前
C++ 红黑树自平衡核心:旋转变色、规则详解与 STL 选型逻辑
数据结构·c++·b树·算法
Navigator_Z8 小时前
LeetCode //C - 1089. Duplicate Zeros
c语言·算法·leetcode