50ms concise python solution


  • 1
    Y
    def sumNumbers(self, root):
        return self.sum_numbers_help(root, 0)
    
    def sum_numbers_help(self, root, curr):
        if not root:
            return 0
    
        curr = 10 * curr + root.val
        if not root.left and not root.right:
            return curr
        return self.sum_numbers_help(root.left, curr) + self.sum_numbers_help(root.right, curr)

Log in to reply
 

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