在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a binary tree, each node has value For all leaves in the tree, consider the numbers represented by the path from the root to that leaf. Return the sum of these numbers modulo Example 1:
Input: [1,0,1,0,1,0,1]
Output: 22
Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
Note:
给出一棵二叉树,其上每个结点的值都是 对树上的每一片叶子,我们都要找出从根到该叶子的路径所表示的数字。 以 示例: 输入:[1,0,1,0,1,0,1] 输出:22 解释:(100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22 提示:
Runtime: 24 ms
Memory Usage: 19 MB
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 } 36ms
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 }
|
请发表评论