-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_270.java
24 lines (21 loc) · 817 Bytes
/
_270.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
package com.fishercoder.solutions.firstthousand;
import com.fishercoder.common.classes.TreeNode;
public class _270 {
public static class Solution1 {
public int closestValue(TreeNode root, double target) {
int val;
int closest = root.val;
while (root != null) {
val = root.val;
closest =
Math.abs(val - target) < Math.abs(closest - target)
|| (Math.abs(val - target) == Math.abs(closest - target)
&& val < closest)
? val
: closest;
root = target < root.val ? root.left : root.right;
}
return closest;
}
}
}