Easy to read solution Python


  • 0
    C
    class Solution(object):
        def sumNumbers(self, root):
            if not root:
                return 0
            return self.sumNumbersRecursive(root, "")
            
        def sumNumbersRecursive(self,root, stringSoFar):
            if not root.left and not root.right:
                return int(stringSoFar+str(root.val))
            summation = 0
            if root.left:
                summation += self.sumNumbersRecursive(root.left, stringSoFar+str(root.val))
            if root.right:
                summation += self.sumNumbersRecursive(root.right, stringSoFar+str(root.val))
            return summation        
    

Log in to reply
 

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