Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
sum-of-root-to-leaf-binary-numbers.py 729 Bytes
Copy Edit Raw Blame History
kamyu authored 2019-04-08 01:13 +08:00 . Create sum-of-root-to-leaf-binary-numbers.py
# Time: O(n)
# Space: O(h)
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def sumRootToLeaf(self, root):
"""
:type root: TreeNode
:rtype: int
"""
M = 10**9 + 7
def sumRootToLeafHelper(root, val):
if not root:
return 0
val = (val*2 + root.val) % M
if not root.left and not root.right:
return val
return (sumRootToLeafHelper(root.left, val) +
sumRootToLeafHelper(root.right, val)) % M
return sumRootToLeafHelper(root, 0)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search