-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0404.go
58 lines (52 loc) · 1.46 KB
/
0404.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Source: https://leetcode.com/problems/sum-of-left-leaves
// Title: Sum of Left Leaves
// Difficulty: Easy
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given the root of a binary tree, return the sum of all left leaves.
//
// A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.
//
// Example 1:
//
// https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg
// Input: root = [3,9,20,null,null,15,7]
// Output: 24
// Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively.
//
// Example 2:
//
// Input: root = [1]
// Output: 0
//
// Constraints:
//
// The number of nodes in the tree is in the range [1, 1000].
// -1000 <= Node.val <= 1000
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func sumOfLeftLeaves(root *TreeNode) int {
return _sumOfLeftLeaves(root, false)
}
func _sumOfLeftLeaves(root *TreeNode, isLeft bool) int {
if root.Left == nil && root.Right == nil {
if isLeft {
return root.Val
}
return 0
}
res := 0
if root.Left != nil {
res += _sumOfLeftLeaves(root.Left, true)
}
if root.Right != nil {
res += _sumOfLeftLeaves(root.Right, false)
}
return res
}