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.若是叶子节点,则返回左子叶的值加上对当前结点的右子节点调用递归的结果;
3.若不是叶子结点,我们对左右子节点分别调用递归函数,返回二者之和;

# Definition for a binary tree node.
# class TreeNode():
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution():
    def sumOfLeftLeaves(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        ans = 0
        l, r = root.left, root.right
        if root:
            if l and (l.left or l.right) is None:
                ans += l.val
            ans += self.sumOfLeftLeaves(l) + self.sumOfLeftLeaves(r)
        return ans
时间: 2024-10-16 13:32:35

404. Sum of Left Leaves (Easy)的相关文章

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

[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

404 Sum of Left Leaves 左叶子之和

计算给定二叉树的所有左叶子之和.示例:    3   / \  9  20    /  \   15   7在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24. 详见:https://leetcode.com/problems/sum-of-left-leaves/description/ C++: 方法一: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *le

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 404 左叶子之和 Sum of Left Leaves

C++ 递归遍历+判断左叶子节点,效率不高, 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

[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

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