• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode1022.从根到叶的二进制数之和|SumofRootToLeafBinaryNumbers ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10667991.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

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 10^9 + 7.

Example 1:

Input: [1,0,1,0,1,0,1]
Output: 22
Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22 

Note:

  1. The number of nodes in the tree is between 1 and 1000.
  2. node.val is 0 or 1.

给出一棵二叉树,其上每个结点的值都是 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. 树中的结点数介于 1 和 1000 之间。
  2. node.val 为 0 或 1 。

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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift的笔记和参考发布时间:2022-07-13
下一篇:
【iOS】Swiftiflet和ifvar发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap