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
    }
};
相关推荐
重生之小比特14 分钟前
【算法拾光】二分查找|二分查找 + 再排序数组中查找元素的第一个和最后一个位置
算法·leetcode·职场和发展
文丑颜不良啊19 分钟前
LeetCode76~100题目解析
java·leetcode
闻缺陷则喜何志丹32 分钟前
vc入门宝典(九)
数据结构·list·mfc·存储·null·class
quaer12 小时前
弗洛伊德算法--判圈法寻找环的入口O(n) O(1)
数据结构·算法
文丑颜不良啊13 小时前
LeetCode51~75题目解析
leetcode
学电子她就能回来吗14 小时前
LeetCode Hot 100|普通数组专题学习笔记(零基础版)
笔记·算法·leetcode
xys_67816 小时前
深挖数据结构:树和二叉树(上)
数据结构·算法
BeiGuTing_code16 小时前
Java动态数组(一)
java·数据结构·windows
文丑颜不良啊18 小时前
LeetCode1~50题目解析
leetcode
Pniubi18 小时前
力扣146.LRU缓存机制
算法·leetcode·缓存