Created
August 30, 2016 19:21
-
-
Save fishercoder1534/c35e86cdde2aa0297134cde62072620b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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