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
    }
};
相关推荐
刃神太酷啦4 小时前
Linux 系统 MySQL 完整安装配置教程:从卸载 MariaDB 到优化 my.cnf----《Hello MySQL!》(1)
android·linux·c语言·c++·mysql·leetcode·mariadb
带多刺的玫瑰4 小时前
Leecode#9刷题之回文数
数据结构·算法
萌动的小火苗5 小时前
数据结构面试题【无答案】
c语言·开发语言·数据结构·链表
tianyu2348 小时前
HashMap 灵魂 30 问:从数据结构到红黑树,从扩容到 ConcurrentHashMap,一篇彻底通关
数据结构·hashmap·hashset·linkedhashmap‌
wenyq78 小时前
LeetCode 2904. Shortest and Lexicographically Smallest Beautiful String
算法·leetcode
_Narcissus_10 小时前
分治&递归
数据结构·c++·笔记·算法·leetcode·递归·分治
不灭的黄金瞳12313 小时前
C语言实现单链表
c语言·数据结构
青 春 记 忆14 小时前
LeetCode 283. 移动零|Python 解法详解
python·算法·leetcode
这是程序猿15 小时前
深入剖析Java HashMap:底层原理、数据结构与核心机制全解析
java·开发语言·数据结构