
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def sumNumbers(self, root):
"""
:type root: TreeNode
:rtype: int
"""
def DFS(root, cur_num_str):
# No valid check, pls only pass valid node in this function
# if there has no left and right child, only append the val itself
if not root.left and not root.right:
return int(cur_num_str + str(root.val))
left_sum = DFS(root.left, cur_num_str + str(root.val)) if root.left else 0
right_sum = DFS(root.right, cur_num_str + str(root.val)) if root.right else 0
return left_sum + right_sum
return DFS(root, '')