★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-kfnocply-md.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a binary tree, each node has value 0
or 1
. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is 0 -> 1 -> 1 -> 0 -> 1
, then this could represent 01101
in binary, which is 13
.node
For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.git
Return the sum of these numbers modulo 10^9 + 7
.github
Example 1:微信
Input: [1,0,1,0,1,0,1]
Output: 22 Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
Note:ide
1
and 1000
.0
or 1
.给出一棵二叉树,其上每一个结点的值都是 0
或 1
。每一条从根到叶的路径都表明一个从最高有效位开始的二进制数。例如,若是路径为 0 -> 1 -> 1 -> 0 -> 1
,那么它表示二进制数 01101
,也就是 13
。this
对树上的每一片叶子,咱们都要找出从根到该叶子的路径所表示的数字。spa
以 10^9 + 7
为模,返回这些数字之和。 code
示例:htm
输入:[1,0,1,0,1,0,1] 输出:22 解释:(100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
提示:
1
和 1000
之间。0
或 1
。1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 var mod:Int = 1000000007 16 var ans:Int = 0 17 func sumRootToLeaf(_ root: TreeNode?) -> Int { 18 ans = 0 19 dfs(root, 0) 20 return ans % mod 21 } 22 23 func dfs(_ cur: TreeNode?,_ v:Int) 24 { 25 if cur == nil {return} 26 if cur?.left == nil && cur?.right == nil 27 { 28 ans += v*2 + cur!.val 29 return 30 } 31 dfs(cur?.left, (v*2 + cur!.val) % mod) 32 dfs(cur?.right, (v*2 + cur!.val) % mod) 33 34 } 35 }
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 let mode = 1000_000_007 16 17 func sumRootToLeaf(_ root: TreeNode?) -> Int { 18 guard let node = root else{ 19 return 0 20 } 21 var answer = 0 22 deepFirst(&answer, node.val, v: node) 23 return answer 24 } 25 26 func deepFirst(_ answer: inout Int,_ current: Int, v : TreeNode){ 27 if v.left == nil && v.right == nil { 28 answer = (answer + current) % mode 29 return 30 } 31 if let lhs = v.left{ 32 deepFirst(&answer, (current * 2 + lhs.val)%mode, v: lhs) 33 } 34 if let rhs = v.right{ 35 deepFirst(&answer, (current * 2 + rhs.val)%mode, v: rhs) 36 } 37 } 38 }
40ms
1 class Solution { 2 private let module = 1_000_000_007 3 4 func sumRootToLeaf(_ root: TreeNode?) -> Int { 5 return helper(root: root, val: 0) % module 6 } 7 8 private func helper(root: TreeNode?, val: Int) -> Int { 9 guard let node = root else { 10 return 0 11 } 12 if node.left == nil && node.right == nil { 13 return (val << 1 + node.val) % module 14 } 15 return helper(root: node.right, val: (val << 1 + node.val) % module) + 16 helper(root: node.left, val: (val << 1 + node.val) % module) 17 } 18 }
44ms
1 class Solution { 2 func sumRootToLeaf(_ root: TreeNode?) -> Int { 3 return sumUp(root, 0) 4 } 5 6 func sumUp(_ node: TreeNode?, _ prefix: Int) -> Int { 7 guard let node = node else { 8 return prefix 9 } 10 let modNum = Int(pow(Double(10), Double(9))) + 7 11 var prefix = ((prefix % modNum) * 2) % modNum + node.val 12 var sum = 0 13 var flag = false 14 if let left = node.left { 15 sum = (sum + sumUp(left, prefix)) % modNum 16 flag = true 17 } 18 if let right = node.right { 19 sum = (sum + sumUp(right, prefix)) % modNum 20 flag = true 21 } 22 if !flag { 23 return prefix 24 } 25 return sum % modNum 26 } 27 }
48ms
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 var sum: Int = 0 16 func sumRootToLeaf(_ root: TreeNode?) -> Int { 17 guard let root = root else { return 0 } 18 dfs(root, []) 19 return sum 20 } 21 22 func dfs(_ node: TreeNode, _ str: [Int]) { 23 guard node.left != nil || node.right != nil else { 24 sum = (sum + convert(str + [node.val])) % 1000000007 25 return 26 } 27 28 if let left = node.left { 29 dfs(left, str + [node.val]) 30 } 31 if let right = node.right { 32 dfs(right, str + [node.val]) 33 } 34 } 35 36 func convert(_ str: [Int]) -> Int { 37 var num: Int = 0 38 39 for n in str { 40 num = (num * 2 + n) % 1000000007 41 } 42 43 return num 44 } 45 }