1022. 从根到叶的二进制数之和

给出一棵二叉树,其上每个结点的值都是 0 或 1 。每一条从根到叶的路径都代表一个从最高有效位开始的二进制数。例如,如果路径为 0 -> 1 -> 1 -> 0 -> 1,那么它表示二进制数 01101,也就是 13 。

对树上的每一片叶子,我们都要找出从根到该叶子的路径所表示的数字。

以 10^9 + 7 为模,返回这些数字之和。

示例:

 

输入:[1,0,1,0,1,0,1]
输出:22
解释:(100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
 

提示:

树中的结点数介于 1 和 1000 之间。
node.val 为 0 或 1 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sum-of-root-to-leaf-binary-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def sumRootToLeaf(self, root: TreeNode) -> int:
        if not root:
            return 0
        self.res=0
        def helper(root,preSum):
            if not root.left and not root.right:
                self.res=(self.res+preSum)% (10 ** 9 + 7)
                return
            if root.left:
                helper(root.left,root.left.val+preSum*2)
            if root.right:
                helper(root.right,root.right.val+preSum*2)
        helper(root,root.val)
        return self.res
原文地址:https://www.cnblogs.com/xxxsans/p/13914497.html