Easy to follow Python solution


  • 0
    A
    class Solution:
    
        sum_ = 0
    
        def traverse(self, root, v = 0):
    
            v = v * 10 + root.val
    
            if not root.left and not root.right:
                self.sum_ += v
                return
    
            if root.left:
                self.traverse(root.left, v)
    
            if root.right:
                self.traverse(root.right, v)
    
        def sumNumbers(self, root):
            """
            :type root: TreeNode
            :rtype: List[float]
            """
            if not root:
                return 0
    
            self.traverse(root)
    
            return self.sum_
    

Log in to reply
 

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