当前位置:大发SEO >> 搜索引擎 >> 搜狗

搜狗搜索怎么加文字的

搜索引擎 搜狗 2025-05-12 1818

摘要:森# 1. 题目 [404. 左叶子之和](https://leetcode-cn.com/problems/sum-of-left-leaves/)难度简单383给定二叉树的根节点 `root` ,返回所有左叶子之和。 示例 1:![img](https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg)输入: root = [3...

森# 1. 题目

搜狗搜索怎么加文字的

[404. 左叶子之和](https://leetcode-cn.com/problems/sum-of-left-leaves/)

难度简单383

给定二叉树的根节点 `root` ,返回所有左叶子之和。

示例 1:

![img](https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg)

输入: root = [3,9,20,null,null,15,7]

输出: 24

解释: 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24

示例 2:

输入: root = [1]

输出: 0

提示:

节点数在 `[1, 1000]` 范围内

`-1000 <= Node.val <= 1000`

2. 题解

3. code

c++

class Solution {

public:

int sum = 0;

void traversal(TreeNode* node) {

if (!node) return;

if (node->left && !node->left->left && !node->left->right) {

sum += node->left->val;

}

traversal(node->left);

traversal(node->right);

return;

}

int sumOfLeftLeaves(TreeNode* root) {

traversal(root);

return sum;

}

};

4. 心得

相关推荐
友情链接