Question:
Give a BST, find a node whose value is closest to the target.
// when calling this method, set minDiff = Integer.MAX_VALUE, prev = null;
public static Node closestValue(Node root, int target, int minDiff, Node prev) {
if (root == null) return prev;
int diffV = Math.abs(root.value - target);
if (diffV > minDiff) return prev;
prev = root;
minDiff = diffV;
if (root.value == target) {
return root;
} else if (root.value > target) {
return closestValue(root.left, target, minDiff, prev);
} else {
return closestValue(root.right, target, minDiff, prev);
}
}