2025每日刷题(244)
Leetcode---404. 左叶子之和

实现代码
go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func sumOfLeftLeaves(root *TreeNode) int {
return dfs(root)
}
func dfs(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
} else {
ans += dfs(root.Left)
}
}
ans += dfs(root.Right)
return ans
}
运行结果

之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!