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
    }
};
相关推荐
Ada's2 小时前
【计算机基础系列】003:Python数据结构
开发语言·数据结构·python
Nil2083 小时前
leetcode 118杨辉三角
算法·leetcode·职场和发展
wanderist.5 小时前
线性筛法详解:从筛质数到欧拉函数、Möbius 函数与约数函数
java·数据结构·算法
All for pursuit.5 小时前
【设计-1】208.实现Trie (前缀树)
数据结构·c++·算法·leetcode
程序员清风7 小时前
哈希表:从键值映射到高效查找
数据结构·哈希算法·散列表
CoderYanger9 小时前
A.每日一题:1614. 括号的最大嵌套深度
java·程序人生·算法·leetcode·面试·学习方法
Logic10110 小时前
C语言/数据结构字符串题解:最短循环节——找出能重复构成原串的最短基础子串
c语言·数据结构·字符串·枚举·时间复杂度·算法题·kmp优化
Xin77010 小时前
LeetCode 230. 二叉搜索树中第 K 小的元素(迭代中序遍历 + 栈)
leetcode
垆边人似月.11 小时前
城市连通性(200分 / 并查集)
数据结构·算法·图论
Gust of wind11 小时前
串与KMP模式匹配:存储结构、基本操作、next数组手算
c语言·数据结构·后端·算法