-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path270-ClosestBinarySearchTreeValue.go
More file actions
154 lines (140 loc) · 4.16 KB
/
270-ClosestBinarySearchTreeValue.go
File metadata and controls
154 lines (140 loc) · 4.16 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
// 270. Closest Binary Search Tree Value
// Given the root of a binary search tree and a target value,
// return the value in the BST that is closest to the target.
// If there are multiple answers, print the smallest.
// Example 1:
// 4
// / \
// 2 5
// / \
// 1 3
// <img src="https://assets.leetcode.com/uploads/2021/03/12/closest1-1-tree.jpg" />
// Input: root = [4,2,5,1,3], target = 3.714286
// Output: 4
// Example 2:
// Input: root = [1], target = 4.428571
// Output: 1
// Constraints:
// The number of nodes in the tree is in the range [1, 10^4].
// 0 <= Node.val <= 10^9
// -10^9 <= target <= 10^9
import "fmt"
import "math"
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// bfs
func closestValue(root *TreeNode, target float64) int {
closest, cur := root, root
for cur != nil {
if target == float64(cur.Val) {
return cur.Val
}
if math.Abs(target - float64(cur.Val)) < math.Abs(target - float64(closest.Val)) {
closest = cur
} else if (math.Abs(target - float64(cur.Val)) == math.Abs(target - float64(closest.Val))) { // 处理值差值一样的情况
if cur.Val < closest.Val { // 谁小选择谁
closest = cur
}
}
if target < float64(cur.Val) {
cur = cur.Left
} else {
cur = cur.Right
}
}
return closest.Val
}
// dfs
func closestValue1(root *TreeNode, target float64) int {
closest := root
var dfs func(*TreeNode,float64)
dfs = func(node *TreeNode,target float64) {
if node == nil {
return
}
dfs(node.Left,target)
if math.Abs(target - float64(node.Val)) < math.Abs(target - float64(closest.Val)) {
closest = node
} else if (math.Abs(target - float64(node.Val)) == math.Abs(target - float64(closest.Val))) { // 处理值差值一样的情况
if node.Val < closest.Val { // 谁小选择谁
closest = node
}
}
dfs(node.Right,target)
}
dfs(root,target)
return closest.Val
}
func closestValue2(root *TreeNode, target float64) int {
diff := func ( a, b float64) float64 {
if a > b {
return a - b
} else {
return b - a
}
}
closest, bl, a := root, diff(float64(root.Val), target), 0.0
for root != nil {
a = diff(float64(root.Val), target)
if a < bl {
closest = root
bl = a
}
if target > float64(root.Val) {
root = root.Right
} else {
root = root.Left
}
}
return closest.Val
}
func main() {
// Example 1:
// 4
// / \
// 2 5
// / \
// 1 3
// <img src="https://assets.leetcode.com/uploads/2021/03/12/closest1-1-tree.jpg" />
// Input: root = [4,2,5,1,3], target = 3.714286
// Output: 4
tree1 := &TreeNode {
4,
&TreeNode{2, &TreeNode{1, nil, nil }, &TreeNode{3, nil, nil }, },
&TreeNode{5, nil, nil },
}
fmt.Println(closestValue(tree1, 3.714286)) // 4
// Example 2:
// Input: root = [1], target = 4.428571
// Output: 1
tree2 := &TreeNode{1, nil, nil }
fmt.Println(closestValue(tree2, 4.428571)) // 1
// tree3 := &TreeNode {
// 4,
// &TreeNode{2, &TreeNode{1, nil, nil }, &TreeNode{3, nil, nil }, },
// &TreeNode{5, nil, nil },
// }
fmt.Println(closestValue(tree1, 3.5)) // 3
fmt.Println(closestValue(tree1, 4.5)) // 5
fmt.Println(closestValue1(tree1, 3.714286)) // 4
fmt.Println(closestValue1(tree2, 4.428571)) // 1
fmt.Println(closestValue1(tree1, 3.5)) // 3
fmt.Println(closestValue1(tree1, 4.5)) // 5
fmt.Println(closestValue2(tree1, 3.714286)) // 4
fmt.Println(closestValue2(tree2, 4.428571)) // 1
fmt.Println(closestValue2(tree1, 3.5)) // 3
fmt.Println(closestValue2(tree1, 4.5)) // 5
}