Files
LeetCode-Go/leetcode/0404.Sum-of-Left-Leaves/404. Sum of Left Leaves.go
2022-09-08 14:55:40 -07:00

28 lines
542 B
Go

package leetcode
import (
"github.com/halfrost/leetcode-go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* 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
}
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
return root.Left.Val + sumOfLeftLeaves(root.Right)
}
return sumOfLeftLeaves(root.Left) + sumOfLeftLeaves(root.Right)
}