[Swift]LeetCode129. 求根到葉子節點數字之和 | Sum Root to Leaf Numbers

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-pkgbjpsw-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.node

An example is the root-to-leaf path 1->2->3 which represents the number 123.git

Find the total sum of all root-to-leaf numbers.github

Note: A leaf is a node with no children.微信

Example:app

Input: [1,2,3]
    1
   / \
  2   3
Output: 25
Explanation:
The root-to-leaf path  represents the number .
The root-to-leaf path  represents the number .
Therefore, sum = 12 + 13 = .1->2121->31325

Example 2:spa

Input: [4,9,0,5,1]
    4
   / \
  9   0
 / \
5   1
Output: 1026
Explanation:
The root-to-leaf path  represents the number 495.
The root-to-leaf path  represents the number 491.
The root-to-leaf path  represents the number 40.
Therefore, sum = 495 + 491 + 40 = .4->9->54->9->14->01026

給定一個二叉樹,它的每一個結點都存放一個 0-9 的數字,每條從根到葉子節點的路徑都表明一個數字。code

例如,從根到葉子節點路徑 1->2->3 表明數字 123htm

計算從根到葉子節點生成的全部數字之和。blog

說明: 葉子節點是指沒有子節點的節點。

示例 1:

輸入: [1,2,3]
    1
   / \
  2   3
輸出: 25
解釋:
從根到葉子節點路徑  表明數字 .
從根到葉子節點路徑  表明數字 .
所以,數字總和 = 12 + 13 = .1->2121->31325

示例 2:

輸入: [4,9,0,5,1]
    4
   / \
  9   0
 / \
5   1
輸出: 1026
解釋:
從根到葉子節點路徑  表明數字 495.
從根到葉子節點路徑  表明數字 491.
從根到葉子節點路徑  表明數字 40.
所以,數字總和 = 495 + 491 + 40 = .4->9->54->9->14->01026

12ms
 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     func sumNumbers(_ root: TreeNode?) -> Int {
16         guard let root = root else { return 0 }
17         var sum = 0
18         var stack: [(node: TreeNode, accum: Int)] = [(node: root, accum: 0)]
19         
20         while !stack.isEmpty {
21             let currentVal = stack.removeLast()
22             let updatedAccum = currentVal.accum * 10 + currentVal.node.val
23             
24             guard currentVal.node.left != nil || currentVal.node.right != nil else {
25                 sum += updatedAccum
26                 continue
27             }
28             
29             if let leftNode = currentVal.node.left {
30                 stack.append((node: leftNode, accum: updatedAccum))
31             }
32             
33             if let rightNode = currentVal.node.right {
34                 stack.append((node: rightNode, accum: updatedAccum))
35             }
36         }
37         
38         return sum
39     }
40     
41 }

16ms

 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     func sumNumbers(_ root: TreeNode?) -> Int {
16         if root == nil { return 0 }
17         else {
18             let result = getNumber(root!, 0)
19             return result
20         }
21     }
22     
23     func getNumber(_ node: TreeNode, _ number: Int) -> Int {
24         let currentNumber = number * 10 + node.val
25         var result = 0
26         if node.left == nil && node.right == nil {
27             return currentNumber
28         } else if node.left == nil {
29             result = getNumber(node.right!, currentNumber)
30         } else if node.right == nil {
31             result = getNumber(node.left!, currentNumber)
32         } else {
33             result = getNumber(node.left!, currentNumber) + getNumber(node.right!, currentNumber)
34         }
35         return result
36     }
37 }

20ms

 1 class Solution {
 2     func sumNumbers(_ root: TreeNode?) -> Int {
 3         let nums = numbers(root)
 4         print(nums)
 5         return nums.reduce(0, { $0 + Int($1)! })
 6     }
 7     
 8     func numbers(_ root: TreeNode?) -> [String] {
 9         guard let root = root else { return [] }
10         
11         if root.left == nil && root.right == nil {
12             return [String(root.val)]
13         }
14         
15         return (numbers(root.left) + numbers(root.right)).map { String(root.val) + $0 }
16     }
17 }

24ms

 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     func sumNumbers(_ root: TreeNode?) -> Int {
16             var sum = 0
17             treeShowNodeVal(root: root, item: "", sum: &sum)
18             return sum
19         }
20         func treeShowNodeVal(root: TreeNode?, item: String, sum: inout Int){
21             if root == nil {
22                 return
23             }
24             var newItem = item
25             newItem.append(String((root?.val)!))
26             if root?.left == nil && root?.right == nil {
27                 let itemValue = Int(newItem)
28                 sum = sum + itemValue!
29             }
30             treeShowNodeVal(root: root?.left, item: newItem, sum: &sum)
31             treeShowNodeVal(root: root?.right, item: newItem, sum: &sum)
32         }
33 }

28ms

 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     func sumNumbers(_ root: TreeNode?) -> Int {
16         var array = [String]()
17         guard let root = root else { return 0 }
18         
19         func helper(_ node: TreeNode, _ temp: String) {
20             var temp = temp + "\(node.val)"
21             if node.left == nil && node.right == nil {
22                 array.append(temp)
23             } else {
24                 if let left = node.left {
25                     helper(left, temp)    
26                 }
27                 if let right = node.right {
28                     helper(right, temp)    
29                 }
30             }
31         }
32         
33         helper(root, "")
34         return array.reduce(0, {return $0 + Int($1)!})
35     }
36 }
相關文章
相關標籤/搜索