-
Notifications
You must be signed in to change notification settings - Fork 2
/
ClosestValueInBST.java
60 lines (51 loc) · 1.38 KB
/
ClosestValueInBST.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package Leetcode;
import com.sun.source.tree.Tree;
/**
* @author kalpak
*
* Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.
*
* Note:
*
* Given target value is a floating point.
* You are guaranteed to have only one unique value in the BST that is closest to the target.
* Example:
*
* Input: root = [4,2,5,1,3], target = 3.714286
*
* 4
* / \
* 2 5
* / \
* 1 3
*
* Output: 4
*
*/
public class ClosestValueInBST {
public static int closestValue(TreeNode root, double target) {
int result = root.val;
int currentNodeVal = 0;
// Binary Search approach
while(root != null) {
currentNodeVal = root.val;
// update the result
if(Math.abs(currentNodeVal - target) < Math.abs(result - target))
result = currentNodeVal;
// either go left or right
if(target < root.val)
root = root.left;
else
root = root.right;
}
return result;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(4);
root.left = new TreeNode(2);
root.right = new TreeNode(5);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(3);
System.out.println(closestValue(root, 3.714286));
}
}