sum-of-left-leaves
This commit is contained in:
parent
98f8727286
commit
7f687781b3
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue