func sumOfLeftLeaves(_ root: TreeNode?) -> Int {
var sum = 0
helper(root, &sum)
return sum
}
private func helper(_ node: TreeNode?, _ sum: inout Int) {
if node == nil {
return
}
if node?.left != nil && node?.left?.left == nil && node?.left?.right == nil {
sum += (node?.left?.val)!
}
helper(node?.left, &sum)
helper(node?.right, &sum)
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme