Find the sum of all left leaves in a given binary tree.
Example:
3
/ \
9 20
/ \
15 7
There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
求所有左叶子节点的和
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func sumOfLeftLeaves(root *TreeNode) int {
if root == nil || (root.Left == nil && root.Right == nil) {
return 0
}
return helper(root.Left, true) + helper(root.Right, false)
}
func helper(root *TreeNode, isLeft bool ) int {
if root == nil{
return 0
}
res := 0
if root.Left == nil && root.Right == nil && isLeft{
res = root.Val
}
res += helper(root.Left, true)
res += helper(root.Right, false)
return res
}