Can anybody tell me why my solution is wrong? Python 8 lines


  • 0
    Z

    I can't pass the test case [1,2,3,4,5], my output is 6, but expected one is 4. I even don't know what the tree will be look like by giving me [1,2,3,4,5].

    Pleas help me to find my mistake. Thank you!

    class Solution(object):
        def sumOfLeftLeaves(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            if not root:
                return 0
            return self.leftLeaves(root.left, True) + self.leftLeaves(root.right, False)       
            
        def leftLeaves(self, root, is_left):
            if not root:
                return 0
            it_val = root.val if is_left else 0
            return it_val + self.leftLeaves(root.left, True) + self.leftLeaves(root.right, False)
    

  • 1
    P

    It looks like this:

    1
    / \
    2 3
    / \
    4 5


  • 0
    Z

    @path I got it! So I misunderstood the question, right? LEAVES are those in the bottom ones! Oh my! Thank you very much!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.