sum-of-left-leaves

This commit is contained in:
Gleb Koval 2021-11-05 16:02:35 +00:00
parent 98f8727286
commit 7f687781b3
No known key found for this signature in database
GPG Key ID: 7C89CDC822F8392B
1 changed files with 23 additions and 0 deletions

23
sum-of-left-leaves/sol.go Normal file
View File

@ -0,0 +1,23 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
// Time: O(n)
// Space: O(1)
func sumOfLeftLeaves(root *TreeNode) int {
return soll(root, false)
}
func soll(root *TreeNode, include bool) int {
if root == nil {
return 0
}
if root.Left == nil && root.Right == nil && include {
return root.Val
}
return soll(root.Left, true) + soll(root.Right, false)
}