Contest 131 - Prob 2 Sum of Root

2019-04-07  本文已影响0人  人树杨
class Solution:
    def sumRootToLeaf(self, root: TreeNode) -> int:
        def dfs(node, total):
            if not node: return
            total = total * 2 + node.val
            if not node.left and not node.right: self.sum += total
            dfs(node.left, total)
            dfs(node.right, total)
        self.sum = 0    
        dfs(root, 0)
        return self.sum % (10**9 + 7)
上一篇 下一篇

猜你喜欢

热点阅读