Golang | Leetcode Golang题解之第404题左叶子之和
题目:
题解:
func isLeafNode(node *TreeNode) bool {
return node.Left == nil && node.Right == nil
}
func sumOfLeftLeaves(root *TreeNode) (ans int) {
if root == nil {
return
}
q := []*TreeNode{root}
for len(q) > 0 {
node := q[0]
q = q[1:]
if node.Left != nil {
if isLeafNode(node.Left) {
ans += node.Left.Val
} else {
q = append(q, node.Left)
}
}
if node.Right != nil && !isLeafNode(node.Right) {
q = append(q, node.Right)
}
}
return
}