-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path404.左叶子之和.go
66 lines (64 loc) · 1.33 KB
/
404.左叶子之和.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
59
60
61
62
63
64
65
/*
* @lc app=leetcode.cn id=404 lang=golang
*
* [404] 左叶子之和
*
* https://leetcode-cn.com/problems/sum-of-left-leaves/description/
*
* algorithms
* Easy (49.93%)
* Likes: 77
* Dislikes: 0
* Total Accepted: 7.1K
* Total Submissions: 14.2K
* Testcase Example: '[3,9,20,null,null,15,7]'
*
* 计算给定二叉树的所有左叶子之和。
*
* 示例:
*
*
* 3
* / \
* 9 20
* / \
* 15 7
*
* 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
*
*
*
*/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func sumOfLeftLeaves(root *TreeNode) int {
return sum(root, false)
// if root == nil || (root.Left == nil && root.Right == nil) {
// return 0
// }
// if root.Left != nil && root.Right == nil {
// return root.Left.Val + sumOfLeftLeaves(root.Left)
// }
// if root.Right != nil && root.Left == nil {
// return sumOfLeftLeaves(root.Right)
// }
// return root.Left.Val + sumOfLeftLeaves(root.Left) + sumOfLeftLeaves(root.Right)
}
func sum(node *TreeNode, isLeft bool) int {
if node == nil {
return 0
}
if node.Left == nil && node.Right == nil {
if isLeft {
return node.Val
}
return 0
}
return sum(node.Left, true) + sum(node.Right, false)
}