My Python solution dfs


  • -1
    L
    class Solution(object):
        def sumNumbers(self, root):
            num, self.Total=0,0
            return self.dfs(root, num)
    
            
        def dfs(self, root, num):
            if not root:
                return 0
            else:
                num*=10
                num+=root.val
                if not root.left and not root.right:
                    self.Total+=num
                for item in [root.left, root.right]:
                    if item:
                        self.dfs(item, num)
                return self.Total

  • 0

    returning a class member makes this code look bad and unsafe.


Log in to reply
 

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