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
    }
};
相关推荐
charlie1145141911 小时前
deque、list 与 forward_list:vector 之外的三个选择
开发语言·数据结构·c++·list·开源项目
动词ing1 小时前
【学习笔记】数据结构(数组滑动窗口+无重复字符的最长字串题目)
数据结构·笔记·学习
拓人间精准客1 小时前
ToB 销售获客避坑指南:如何用全维度大数据实现降本增效
大数据·数据结构·单例模式
小七在进步2 小时前
数据结构:快速排序
数据结构·算法·排序算法
土司大王2 小时前
LeetCode hot100——二叉树的最大深度
算法·leetcode·职场和发展
圣保罗的大教堂2 小时前
leetcode 3720. 大于目标字符串的最小字典序排列 中等
leetcode
lzx_0022 小时前
list(全)
数据结构·list
Tisfy8 小时前
LeetCode 1927.求和游戏:抵消+看最值
java·leetcode·游戏·题解·博弈论
玖玥拾8 小时前
LeetCode 125 验证回文串
算法·leetcode
玖玥拾17 小时前
LeetCode 392 判断子序列
笔记·算法·leetcode