[Swift]LeetCode1022. 從根到葉的二進制數之和 | Sum of Root To Leaf Binary Numbers

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(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. 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 。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. 樹中的結點數介於 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 }
相關文章
相關標籤/搜索