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
    }
};
相关推荐
UIU1149 小时前
P1233 [ICPC 2001 Taejon R] 木棍加工
数据结构·c++·算法·动态规划
niyongsheng11 小时前
3D 装箱算法工程化:数据结构、放置启发式与支撑规则
数据结构·算法·rust
叉烧滑蛋13 小时前
B站台湾大学郭彦甫|MATLAB 学习笔记|04 变量与档案存取
数据结构
j7~14 小时前
【穷举、深搜与回溯算法】一棵决策树讲透全排列与子集
数据结构·c++·算法·leetcode·回溯·递归
Shaoshing15 小时前
LeetCode100 42.接雨水
java·算法·leetcode
文丑颜不良啊17 小时前
LeetCode1261~1280题目解析
java·leetcode
not coder17 小时前
一文讲通--从树到红黑树
数据结构·算法
爱吃香菜的初学者17 小时前
八.数据结构——哈希表(1)
数据结构·散列表
Shaoshing17 小时前
LeetCode100 11. 盛最多水的容器
算法·leetcode
海绵宝宝转agent18 小时前
Leetcode二叉树算法个人理解
笔记·学习·算法·leetcode·深度优先