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
    }
};
相关推荐
Nil2089 小时前
leetcode 763划分字母区间
算法·leetcode·职场和发展
不会就选b9 小时前
算法日常・每日刷题--<动态规划>20
算法·leetcode·动态规划
不会就选b10 小时前
算法日常・每日刷题--<动态规划>19
算法·leetcode·职场和发展
垆边人似月.11 小时前
华为机试题 :和为 K 的子数组个数
算法·leetcode·华为
夏幻灵12 小时前
每日一题:LeetCode 165:比较版本号
算法·leetcode·职场和发展
Navigator_Z12 小时前
LeetCode //C - 1288. Remove Covered Intervals
c语言·算法·leetcode
0+11114 小时前
算法 --链表
数据结构·算法·leetcode·链表
圣保罗的大教堂14 小时前
leetcode 2333. 最小差值平方和 中等
leetcode
夜之眷属16 小时前
PriorityQueue:for-each 遍历和 poll 取出,顺序一样吗?
java·服务器·数据结构·后端·算法
思维新观察17 小时前
三星 Galaxy Tab S12 系列:大屏 AI 平板,兼顾轻薄与生产力
数据结构·人工智能·电脑