[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 }

原文地址:https://www.cnblogs.com/strengthen/p/9782487.html

时间: 2024-11-02 12:25:12

[Swift]LeetCode404. 左叶子之和 | Sum of Left Leaves的相关文章

leetcode 404 左叶子之和 Sum of Left Leaves

C++ 递归遍历+判断左叶子节点,效率不高, 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int

404 Sum of Left Leaves 左叶子之和

计算给定二叉树的所有左叶子之和.示例:    3   / \  9  20    /  \   15   7在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24. 详见:https://leetcode.com/problems/sum-of-left-leaves/description/ C++: 方法一: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *le

【leetcode 简单】 第九十四题 左叶子之和

计算给定二叉树的所有左叶子之和. 示例: 3 / 9 20 / 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def sumOfLeft

[Swift]LeetCode371. 两整数之和 | Sum of Two Integers

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example:Given a = 1 and b = 2, return 3. Credits:Special thanks to @fujiaozhu for adding this problem and creating all test cases. 不使用运算符 + 和 - ???????,计算

[Swift]LeetCode834. 树中距离之和 | Sum of Distances in Tree

An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0]and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Exam

LeetCode: 404.左叶子节点

计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 解析 我们需要找到这样的节点 属于叶子节点 属于父节点的左子节点 方法一:用栈,dfs遍历,用全局变量res作为累积和.遍历的过程中传递该节点是否是左子节点.同时判断左右子节点是否为None,则可以知道是不是左叶子节点. class Solution: def sumOfLeftLeaves(self, root: TreeNode) -> int

LeetCode 404. 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. 题目标签:Tree 这道题目给了我们一个二叉树,让我们找到所有左子叶之和.这里需要另外一个function - sumLeftLeaves 还要一个int

[LeetCode] 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. 这道题让我们求一棵二叉树的所有左子叶的和,那么看到这道题我们知道这肯定是考二叉树的遍历问题,那么最简洁的写法肯定是用递归,由于我们只需要累加左子叶之和,那

leetcode-404. Sum of Left Leaves

404. 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.求所有的左叶子节点的和java代码: /** * Definition for a binary tree