-
Notifications
You must be signed in to change notification settings - Fork 0
/
404.go
60 lines (49 loc) · 1.05 KB
/
404.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
# ------------------------------ #
# #
# version 0.0.1 #
# #
# Aleksiej Ostrowski, 2023 #
# #
# https://aleksiej.com #
# #
# ------------------------------ #
*/
package main
import (
"fmt"
)
/**
* Definition for a binary tree node.
**/
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func sumOfLeftLeaves_recc(root *TreeNode, what int) int {
if root == nil {
return 0
}
var res int = 0
if (root.Left == nil) && (root.Right == nil) && (what == 1) {
res += root.Val
}
if root.Left != nil {
res += sumOfLeftLeaves_recc(root.Left, 1)
}
if root.Right != nil {
res += sumOfLeftLeaves_recc(root.Right, 2)
}
return res
}
func sumOfLeftLeaves(root *TreeNode) int {
return sumOfLeftLeaves_recc(root, 0)
}
func main() {
tree1 := &TreeNode{1, nil, nil}
tree2 := &TreeNode{2, nil, nil}
tree := &TreeNode{0, tree1, tree2}
res := sumOfLeftLeaves(tree)
fmt.Println(res)
}