Skip to content

Commit c34f34f

Browse files
author
arbent
committed
2018/9/21
1 parent 351ae08 commit c34f34f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* 404. 左叶子之和
2+
计算给定二叉树的所有左叶子之和。
3+
4+
示例:
5+
6+
3
7+
/ \
8+
9 20
9+
/ \
10+
15 7
11+
12+
在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 */
13+
/**
14+
* Definition for a binary tree node.
15+
* type TreeNode struct {
16+
* Val int
17+
* Left *TreeNode
18+
* Right *TreeNode
19+
* }
20+
*/
21+
func sumOfLeftLeaves(root *TreeNode) int {
22+
if root == nil || (root.Left == nil && root.Right == nil) {
23+
return 0
24+
}
25+
if root.Left == nil {
26+
return sumOfLeftLeaves(root.Right)
27+
}
28+
if root.Left.Left == nil && root.Left.Right == nil {
29+
return root.Left.Val + sumOfLeftLeaves(root.Right)
30+
}
31+
if root.Right == nil {
32+
return sumOfLeftLeaves(root.Left)
33+
}
34+
return sumOfLeftLeaves(root.Left) + sumOfLeftLeaves(root.Right)
35+
}

0 commit comments

Comments
 (0)