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
    }
};
相关推荐
D e re k ZH2 小时前
C++ 系统学习手记:从语法细节到工程实践
开发语言·数据结构·c++·算法
文丑颜不良啊5 小时前
LeetCode2261~2280题目解析
java·leetcode
zh_xuan8 小时前
LeeCode 496. 下一个更大元素I
数据结构·算法·栈·单调栈
文丑颜不良啊11 小时前
LeetCode2241~2260题目解析
java·leetcode
文丑颜不良啊11 小时前
LeetCode2321~2340题目解析
java·leetcode
白杨尚青12 小时前
C++进阶篇(五):红黑树——四条规则、三种插入情况与旋转变色
开发语言·数据结构·c++·stl·红黑树·平衡二叉树
likexuec1 天前
数据结构(2)
数据结构
文丑颜不良啊1 天前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z1 天前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
D e re k ZH1 天前
C++ 标准库算法全景指南
数据结构·c++·算法