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

[Swift]LeetCode404.左叶子之和|SumofLeftLeaves

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

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

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

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

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

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 }

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
[Swift]八大排序算法(一):冒泡排序发布时间:2022-07-13
下一篇:
[转]swift学习资源大集合发布时间: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