270 - Closest Binary Search Tree Value
Written on October 30, 2015
Tweet
Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.
public class Solution {
public int closestValue(TreeNode root, double target) {
Stack<treenode> stack = new Stack<treenode>();
int result = root.val;
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
root = root.left;
} else {
root = stack.pop();
if (Math.abs(root.val - target) < Math.abs(result - target)) {
result = root.val;
}
root = root.right;
}
}
return result;
}
}
class Solution(object):
def closestValue(self, root, target):
"""
:type root: TreeNode
:type target: float
:rtype: int
"""
ret = root.val
while root:
if abs(ret - target) > abs(root.val - target):
ret = root.val
root = root.right if root.val <= target else root.left
return ret