题解 | #左叶子之和# | Golang
左叶子之和
https://www.nowcoder.com/practice/405a9033800b403ba8b7b905bab0463d
package main import ( . "nc_tools" ) /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ func sumOfLeftLeaves( root *TreeNode ) int { if root == nil { return 0 } ans := 0 if root.Left != nil { if root.Left.Left == nil && root.Left.Right == nil { ans += root.Left.Val } } return ans + sumOfLeftLeaves(root.Left) + sumOfLeftLeaves(root.Right) }