forked from szl0072/Leetcode-Solution-Code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClosestBinarSearchTreeValue.java
43 lines (38 loc) · 1.21 KB
/
ClosestBinarSearchTreeValue.java
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
package leetcode;
/**
* Project Name : Leetcode
* Package Name : leetcode
* File Name : ClosestBinarSearchTreeValue
* Creator : Edward
* Date : Oct, 2017
* Description : 270. Closest Binary Search Tree Value
*/
public class ClosestBinarSearchTreeValue {
// time : O(n) space : O(1)
public int closestValue(TreeNode root, double target) {
int res = root.val;
while (root != null) {
if (Math.abs(target - root.val) < Math.abs(target - res)) {
res = root.val;
}
root = root.val > target ? root.left : root.right;
}
return res;
}
// time : O(n) space : O(n)
public int closestValue2(TreeNode root, double target) {
return helper(root, target, root.val);
}
public int helper(TreeNode root, double target, int val) {
if (root == null) return val;
if (Math.abs(root.val - target) < Math.abs(val - target)) {
val = root.val;
}
if (root.val < target) {
val = helper(root.right, target, val);
} else if (root.val > target) {
val = helper(root.left, target, val);
}
return val;
}
}