[Swift]LeetCode404. 左叶子之和 | Sum of Left Leaves

Find the sum of all left leaves in a given binary tree.

Example:

    3
   / \
  9  20
    /  \
   15   7

There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.

计算给定二叉树的所有左叶子之和。

示例:

    3
   / \
  9  20
    /  \
   15   7

在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24

 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 sumOfLeftLeaves(_ root: TreeNode?) -> Int {
17         if root == nil {return sum}
18         if root!.left != nil
19         {
20             if root!.left!.left == nil && root!.left!.right == nil
21             {
22                 sum += root!.left!.val
23             }
24         }
25         //递归
26         sumOfLeftLeaves(root!.left)
27         sumOfLeftLeaves(root!.right)
28         return sum
29     }
30 }

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 sumOfLeftLeaves(_ root: TreeNode?) -> Int {
16         return sumOfLeftLeaves(root, false)
17     }
18     
19     func sumOfLeftLeaves(_ node: TreeNode?, _ isLeft: Bool) -> Int {
20         guard let node = node else { return 0 }
21         if node.left == nil && node.right == nil && isLeft {
22             return node.val
23         }
24         
25         return sumOfLeftLeaves(node.left, true) + sumOfLeftLeaves(node.right, false)
26     }
27 }

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     var sum = 0
16 
17     func sumOfLeftLeaves(_ root: TreeNode?) -> Int {
18         if root == nil || (root?.left == nil && root?.right == nil) {
19             return 0
20         }
21         
22         search(root)
23         
24         return sum
25     }
26     
27     func search(_ root: TreeNode?) -> Void {
28         if root == nil {
29             return
30         }
31 
32         if isLeave(root?.left) {
33             let val = root?.left?.val ?? 0
34             sum = sum + val
35         } else {
36             search(root?.left)
37         }
38         
39         if isLeave(root?.right) {
40             return
41         } else {
42             search(root?.right)
43         }
44     }
45     
46     func isLeave(_ root: TreeNode?) -> Bool {
47         if root == nil {
48             return true
49         }
50         
51         if root?.left == nil && root?.right == nil {
52             return true
53         }
54         
55         return false
56     }
57 }

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

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 sumOfLeftLeaves(_ root: TreeNode?) -> Int {
16             var sum = 0
17             toSumLeftTreeNode(root: root, sum: &sum)
18             return sum
19         }
20         func toSumLeftTreeNode(root: TreeNode?, sum: inout Int){
21             if root == nil {
22                 return
23             }
24             if root?.left != nil && (root?.left?.left == nil && root?.left?.right == nil){
25                 sum = sum + (root?.left?.val)!
26             }
27             toSumLeftTreeNode(root: root?.left, sum: &sum)
28             toSumLeftTreeNode(root: root?.right, sum: &sum)
29         }
30 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/9782487.html