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
|
func sumOfLeftLeaves(root *TreeNode) int { res := 0 if root == nil { return 0 } leftNode := root.Left leftNodeVal := sumOfLeftLeaves(root.Left) rightNodeVal := sumOfLeftLeaves(root.Right) if leftNode != nil && leftNode.Left == nil && leftNode.Right == nil { leftNodeVal = leftNode.Val } res = leftNodeVal + rightNodeVal return res }
|