404 sum of left leaves
·data-structure-and-algorithm
#binary-tree
404. 左叶子之和
go:
/**
* 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
}
var sum int
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
sum += root.Left.Val
}
sum += sumOfLeftLeaves(root.Left)
sum += sumOfLeftLeaves(root.Right)
return sum
}