Sum of Left Leaves

Find the sum of all left leaves in a given binary tree.

Example:

    3
   /   9  20
    /     15   7

There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.

Analyse: Check if the current node has left child and if the left child is a leaf node.

Runtime: 3ms

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int sumOfLeftLeaves(TreeNode* root) {
13         if (!root || (!root->left && !root->right)) return 0;
14
15         int result = 0;
16         sumLeftLeaves(root, result);
17         return result;
18     }
19
20     void sumLeftLeaves(TreeNode* root, int &result) {
21         if (root->left && !root->left->left && !root->left->right)
22             result += root->left->val;
23
24         if (root->left) sumLeftLeaves(root->left, result);
25         if (root->right) sumLeftLeaves(root->right, result);
26     }
27 };
时间: 2024-11-05 12:09:22

Sum of Left Leaves的相关文章

leetcode-404. Sum of Left Leaves

404. Sum of Left Leaves Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.求所有的左叶子节点的和java代码: /** * Definition for a binary tree

[LeetCode] Sum of Left Leaves

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 题目要求树的所有左叶节点的和,根据题意判断出所有左叶节点,使用递归即可. class Solution { public: int sumOfLeftLe

LeetCode 404. Sum of Left Leaves (左子叶之和)

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 题目标签:Tree 这道题目给了我们一个二叉树,让我们找到所有左子叶之和.这里需要另外一个function - sumLeftLeaves 还要一个int

404. Sum of Left Leaves

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 思路:到leaf的时候判断是不是left leaf.用一个boolean function来记录搜索的是左枝还是右枝. /** * Definition

404. Sum of Left Leaves (Easy)

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 题意:计算所有左叶子的和思路:遍历二叉树 1.检查当前节点的左子节点是否是左子叶:2.若是叶子节点,则返回左子叶的值加上对当前结点的右子节点调用递归的结果

[LeetCode] Sum of Left Leaves 左子叶之和

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 这道题让我们求一棵二叉树的所有左子叶的和,那么看到这道题我们知道这肯定是考二叉树的遍历问题,那么最简洁的写法肯定是用递归,由于我们只需要累加左子叶之和,那

[Swift]LeetCode404. 左叶子之和 | Sum of Left Leaves

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 计算给定二叉树的所有左叶子之和. 示例: 3 / 9 20 / 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 1 /**

[LeetCode&Python] Problem 404. Sum of Left Leaves

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. # Definition for a binary tree node. # class TreeNode(object): # def __init__

[LC] 404. Sum of Left Leaves

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. Solution 1:BFS /** * Definition for a binary tree node. * public class TreeNo