Knowee
Questions
Features
Study Tools

Given the root of a binary tree, return the sum of all left leaves.A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.

Question

Given the root of a binary tree, return the sum of all left leaves.A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python solution for the problem:

class Solution(object):
    def sumOfLeftLeaves(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        elif self.isLeaf(root.left):
            return root.left.val + self.sumOfLeftLeaves(root.right)
        else:
            return self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)

    def isLeaf(self, node):
        if not node:
            return False
        if not node.left and not node.right:
            return True
        return False

Here is a step by step explanation of the code:

  1. We define a class Solution with two methods: sumOfLeftLeaves and isLeaf.
  2. sumOfLeftLeaves is the main function that takes the root of the binary tree as input and returns the sum of all left leaves.
  3. If the root is None, we return 0 because there are no leaves in an empty tree.
  4. If the left child of the root is a leaf node, we add its value to the sum of left leaves of the right subtree.
  5. If the left child of the root is not a leaf node, we recursively find the sum of left leaves in the left and right subtrees.
  6. isLeaf is a helper function that checks if a node is a leaf node. A node is a leaf node if it does not have any children. If the node is None or it has any child, it is not a leaf node.

This problem has been solved

Similar Questions

Given a Binary Tree of size N, your task is to complete the function rightLeafSum(), which should return the sum of all the leaf nodes that are the right child of their parent of the given binary tree.

What is a full binary tree?a) Each node has exactly zero or two childrenb) Each node has exactly two childrenc) All the leaves are at the same leveld) Each node has exactly one or two children

Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.A leaf is a node with no children. Example 1:Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22Output: trueExplanation: The root-to-leaf path with the target sum is shown.Example 2:Input: root = [1,2,3], targetSum = 5Output: falseExplanation: There two root-to-leaf paths in the tree:(1 --> 2): The sum is 3.(1 --> 3): The sum is 4.There is no root-to-leaf path with sum = 5.Example 3:Input: root = [], targetSum = 0Output: falseExplanation: Since the tree is empty, there are no root-to-leaf paths. Constraints:The number of nodes in the tree is in the range [0, 5000].-1000 <= Node.val <= 1000-1000 <= targetSum <= 1000

In a full binary tree if number of internal nodes is I, then number of leaves L are?OptionsL = 2*IL = I + 1 L = I – 1L = 2*I – 1

In a binary tree, the node that is directly above another node is called the _____ of that node.a)childb)siblingc)parentd)leaf

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.