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
| // @Title: 左叶子之和 (Sum of Left Leaves)
// @Author: 15816537946@163.com
// @Date: 2019-11-24 21:45:21
// @Runtime: 0 ms
// @Memory: 2.7 MB
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
/*
func sumOfLeftLeaves(root *TreeNode) int {
if root == nil {
return 0
}
if root.Left == nil {
return sumOfLeftLeaves(root.Right)
}
if root.Left.Left == nil && root.Left.Right == nil {
fmt.Println("->", root.Left.Val)
return root.Left.Val + sumOfLeftLeaves(root.Right)
}
return sumOfLeftLeaves(root.Left) + sumOfLeftLeaves(root.Right)
}
*/
func sumOfLeftLeaves(root *TreeNode) int {
if root == nil {
return 0
}
if root.Left == nil {
return sumOfLeftLeaves(root.Right)
}
if root.Left.Left == nil && root.Left.Right == nil {
return root.Left.Val + sumOfLeftLeaves(root.Right)
}
return sumOfLeftLeaves(root.Left) + sumOfLeftLeaves(root.Right)
}
|