# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # @param root, a tree node # @return an integer def crtNum(self, crt, num): if not crt.left and not crt.right: return num*10+crt.val elif not crt.left: return self.crtNum(crt.right,num*10+crt.val) elif not crt.right: return self.crtNum(crt.left,num*10+crt.val) else: return self.crtNum(crt.right,num*10+crt.val)+self.crtNum(crt.left,num*10+crt.val) def sumNumbers(self, root): if root: return self.crtNum(root,0) else: return 0
Tuesday, September 9, 2014
Leetcode: Sum Root to Leaf Numbers @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment