Skip to content

Instantly share code, notes, and snippets.

@fishercoder1534
Created August 30, 2016 19:21
Show Gist options
  • Save fishercoder1534/c35e86cdde2aa0297134cde62072620b to your computer and use it in GitHub Desktop.
Save fishercoder1534/c35e86cdde2aa0297134cde62072620b to your computer and use it in GitHub Desktop.
public int closestValue(TreeNode root, double target) {
long minVal = Long.MAX_VALUE;
while(root != null){
if(Math.abs(root.val - target) < Math.abs(minVal - target)){
minVal = root.val;
}
if(target < root.val) root = root.left;
else root = root.right;
}
return minVal == Long.MAX_VALUE ? 0 : (int) minVal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment