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
    }
};
相关推荐
一叶落4386 小时前
题目:15. 三数之和
c语言·数据结构·算法·leetcode
y = xⁿ6 小时前
【LeetCodehot100】2:两数相加 19 删除链表倒数第n个节点
数据结构·链表
罗湖老棍子7 小时前
【例 1】数列操作(信息学奥赛一本通- P1535)
数据结构·算法·树状数组·单点修改 区间查询
big_rabbit05027 小时前
[算法][力扣222]完全二叉树的节点个数
数据结构·算法·leetcode
张李浩8 小时前
Leetcode 15三题之和
算法·leetcode·职场和发展
x_xbx9 小时前
LeetCode:206. 反转链表
算法·leetcode·链表
abant29 小时前
leetcode 138 复制随机链表
算法·leetcode·链表
ab1515179 小时前
3.17二刷基础112 118 完成进阶52
数据结构·算法
美式请加冰9 小时前
链表的介绍和使用
数据结构·链表
小王不爱笑13210 小时前
排序算法 Java
数据结构·算法·排序算法