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
    }
};
相关推荐
xlp666hub3 小时前
Leetcode第二题:用 C++ 解决字母异位词分组
c++·leetcode
xlp666hub20 小时前
Leetcode第一题:用C++解决两数之和问题
c++·leetcode
任沫1 天前
字符串
数据结构·后端
祈安_1 天前
Java实现循环队列、栈实现队列、队列实现栈
java·数据结构·算法
NineData3 天前
数据库管理工具NineData,一年进化成为数万+开发者的首选数据库工具?
运维·数据结构·数据库
琢磨先生David11 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
qq_4542450311 天前
基于组件与行为的树状节点系统
数据结构·c#
超级大福宝11 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
Charlie_lll11 天前
力扣解题-88. 合并两个有序数组
后端·算法·leetcode
菜鸡儿齐11 天前
leetcode-最小栈
java·算法·leetcode