python solution recursive


  • 0
    G
     def sumNumbers(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            self.result = 0
            self._sumNumbers(root, '')
            return self.result
    
    def _sumNumbers(self, root, s):
            if not root:
                return
            if not root.left and not root.right:
                self.result = self.result + int(s+str(root.val))
                return
            self._sumNumbers(root.left, s+str(root.val))
            self._sumNumbers(root.right, s+str(root.val))
            return
    

Log in to reply
 

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