Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Apr 14, 2024
1 parent b231968 commit 7f1ae86
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions leetcode/404.SumofLeftLeaves/sol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// https://leetcode.com/problems/sum-of-left-leaves

package main

/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/

type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

func sumOfLeftLeaves(root *TreeNode) int {
if root == nil {
return 0
}
res := 0
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
res += root.Left.Val
}
res += sumOfLeftLeaves(root.Left)
res += sumOfLeftLeaves(root.Right)
return res
}

0 comments on commit 7f1ae86

Please sign in to comment.