Skip to content

Instantly share code, notes, and snippets.

@kanglicheng
Created March 26, 2018 23:03
Show Gist options
  • Save kanglicheng/117ad4d4a6030a2ac73144b525a12845 to your computer and use it in GitHub Desktop.
Save kanglicheng/117ad4d4a6030a2ac73144b525a12845 to your computer and use it in GitHub Desktop.
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def sumNumbers(self , root):
if not root:
return 0
return self.sumHelper(root, 0)
def sumHelper(self, root, sum):
if not root:
return 0
sum = sum*10 + root.val
if not root.left and not root.right:
return sum
return self.sumHelper(root.left, sum) + self.sumHelper(root.right, sum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment