-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_270.java
More file actions
43 lines (38 loc) · 1.22 KB
/
Copy pathP_270.java
File metadata and controls
43 lines (38 loc) · 1.22 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
package leetcode.easy;
import utils.DataStructures.TreeNode;
public class P_270 {
public int closestValueIterative(TreeNode root, double target) {
int ret = root.val;
while (root != null) {
if (Math.abs(target - root.val) < Math.abs(target - ret)) {
ret = root.val;
if (Math.abs(target - root.val) <= 0.5) {
break;
}
}
root = root.val > target ? root.left : root.right;
}
return ret;
}
public int closestValue(TreeNode root, double target) {
final int[] res = { Integer.MAX_VALUE };
final double[] diff = { Double.MAX_VALUE };
recurse(root, target, res, diff);
return res[0];
}
private static void recurse(TreeNode node, double target, int[] res, double[] diff) {
if (node == null) {
return;
}
final double currDiff = Math.abs(node.val - target);
if (currDiff < diff[0]) {
res[0] = node.val;
diff[0] = currDiff;
}
if (node.val < target) {
recurse(node.right, target, res, diff);
} else {
recurse(node.left, target, res, diff);
}
}
}