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
    }
};
相关推荐
7yewh1 小时前
SLAM 三维空间刚体运动(2)
数据结构·人工智能·机器人·嵌入式·slam
leikooo2 小时前
用餐厅和盘子理解栈和堆
java·数据结构·链表·rust
Navigator_Z5 小时前
LeetCode //C - 1278. Palindrome Partitioning III
c语言·算法·leetcode
lvwangshu5 小时前
登录日志 题解
数据结构·线段树·题解·技巧·性质题·思维好题·细节题
CQU_JIAKE5 小时前
10.2【A】
数据结构
C++ 老炮儿的技术栈6 小时前
不一样的数值交换
数据结构·c++·人工智能·算法·c·csdn开发云
青山木7 小时前
Hot 100 --- 编辑距离
java·数据结构·算法·leetcode·动态规划
梦帮科技8 小时前
vLLM / TensorRT-LLM 极限推理:PagedAttention 细粒度物理页表管理与连续批处理(Continuous Batching)实战
数据结构·人工智能·分布式·python·深度学习·算法·vllm
H.莓飛10 小时前
【数据结构】链表_OJ题
linux·数据结构·算法·链表
All for pursuit.10 小时前
【回溯-4】46.全排列
数据结构·c++·算法·leetcode