Short python solution


  • 2
    F
    class Solution(object):
    def sumNumbers(self, root):
        if not root: return 0
        self.res=0
        self.cal(root, '')
        return self.res
    
    def cal(self, root, temp):
        if not root: return
        if not root.left and not root.right: self.res+=int(temp+str(root.val))
        for node in [root.left, root.right]: self.cal(node, temp+str(root.val))

  • 0
    Z
    This post is deleted!

Log in to reply
 

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