-
Notifications
You must be signed in to change notification settings - Fork 0
/
671.go
72 lines (58 loc) · 1.25 KB
/
671.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
63
64
65
66
67
68
69
70
71
72
/*
# ------------------------------ #
# #
# version 0.0.1 #
# #
# Aleksiej Ostrowski, 2023 #
# #
# https://aleksiej.com #
# #
# ------------------------------ #
*/
package main
import (
"fmt"
"sort"
)
/**
* Definition for a binary tree node.
**/
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func findSecondMinimumValue(root *TreeNode) int {
keys := make(map[int]int)
findSecondMinimumValue_help(root, keys)
res := make([]int, 0, len(keys))
for key := range keys {
res = append(res, key)
}
keys = nil
sort.Ints(res)
x0 := res[0]
for _, el := range res[1:] {
if x0 != el {
return el
}
}
return -1
}
func findSecondMinimumValue_help(root *TreeNode, keys map[int]int) {
if root == nil {
return
}
keys[root.Val] = 1
findSecondMinimumValue_help(root.Left, keys)
findSecondMinimumValue_help(root.Right, keys)
}
func main() {
tree3 := TreeNode{5, nil, nil}
tree4 := TreeNode{7, nil, nil}
tree2 := TreeNode{5, &tree3, &tree4}
tree1 := TreeNode{2, nil, nil}
tree0 := TreeNode{2, &tree1, &tree2}
fmt.Println(findSecondMinimumValue(&tree0))
fmt.Println("ok")
}