-
Notifications
You must be signed in to change notification settings - Fork 0
/
111.go
62 lines (48 loc) · 1009 Bytes
/
111.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
61
62
/*
# ------------------------------ #
# #
# version 0.0.1 #
# #
# Aleksiej Ostrowski, 2022 #
# #
# https://aleksiej.com #
# #
# ------------------------------ #
*/
package main
import (
"fmt"
)
/**
* Definition for a binary tree node.
**/
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
// https://leetcode.com/problems/minimum-depth-of-binary-tree/solutions/36050/my-simple-recursive-python-solution/
func min(a, b int) int {
if a < b {
return a
} else {
return b
}
}
func minDepth(root *TreeNode) int {
if root == nil {
return 0
}
if root.Left == nil {
return 1 + minDepth(root.Right)
}
if root.Right == nil {
return 1 + minDepth(root.Left)
}
return 1 + min(minDepth(root.Left), minDepth(root.Right))
}
func main() {
tree := &TreeNode{0, nil, nil}
res := minDepth(tree)
fmt.Println(res)
}