# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
"""
integer,string, tuple are immutable, when passed in function they can not be modified in place
immutable objects:
Numeric types: int, float, complex
string
tuple
frozen set
bytes
mutable:
list
dict
set
byte array
"""
class Solution(object):
def sumNumbers(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root : return 0
res=[]
curr=[]
def dfs(node,curr,res):
curr.append(str(node.val))
if not node.left and not node.right:
#print curr
res.append(int(''.join(curr)))
else:
if node.left:
dfs(node.left,curr,res)
curr.pop()
if node.right:
dfs(node.right,curr,res)
curr.pop()
dfs(root,curr,res)
#print res
return sum(res)
网友评论