题解 | #二叉树根节点到叶子节点的所有路径和#
二叉树根节点到叶子节点的所有路径和
http://www.nowcoder.com/practice/185a87cd29eb42049132aed873273e83
helper函数:给定当前结点root和当前的路径和s,返回以root为根节点的树的路径和。
class Solution: def sumNumbers(self , root ): # write code here def helper(root, s): if not root: return 0 s = 10*s + root.val if not root.left and not root.right: return s return helper(root.left, s) + helper(root.right, s) return helper(root, 0)