Created
March 26, 2018 23:03
-
-
Save kanglicheng/117ad4d4a6030a2ac73144b525a12845 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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