/*
* 270. Closest Binary Search Tree Value
Total Accepted: 13710 Total Submissions: 39124 Difficulty: Easy
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.
Hide Company Tags Microsoft Google Snapchat
Hide Tags Tree Binary Search
Hide Similar Problems (M) Count Complete Tree Nodes (H) Closest Binary Search Tree Value II
*/
package tree;
public class ClosestValueBST {
public int closestValue(TreeNode root, double target) {
int ret1 = closestValueIter(root, target); // beat 62.39%
int ret2 = recursive(root, target); //beat 62.39%
return ret1 == ret2 ? ret2 : ret1;
}
public int closestValueIter(TreeNode root, double target) {
/*
* https://leetcode.com/discuss/60089/clean-and-concise-java-solution
* test case:
[4, 2, 6, 1, 3, 5, 7] 4.428571 -> 4
[1, 2], 3.428571 - > 2
[2, 1], 2147483647.0 -> 2
[8, 1], 6.0 - 8
*/
int ret = root.val; // 保持起始的root值,和下次的iteration 做比较
while (root != null) {
if (Math.abs(target - root.val ) < Math.abs( target - ret )) {
ret = root.val;
}
root = root.val > target ? root.left : root.right;
}
return ret;
}
public int recursive(TreeNode root, double target) {
// https://leetcode.com/discuss/54438/4-7-lines-recursive-iterative-ruby-c-java-python
int a = root.val;
TreeNode kid = target > a ? root.right : root.left;
if (kid == null) return a;
int b = recursive(kid, target);
return Math.abs(a - target) < Math.abs(b - target) ? a : b;
// if (Math.abs( root.val - target) <= 0.5) {
// return root.val;
// } else if ( target - (double) root.val > 0.5) {
// return helper(root.right, target, root.val);
// } else if ((double)root.val - target > 0.5){
// return helper(root.left, target, root.val);
// } else {
// System.out.println("here wrong!");
// return lastVal;
// }
}
public static void main(String[] args) {
int[][] nums;
ConvertArrayToBST_108 cbst = new ConvertArrayToBST_108();
ClosestValueBST cv = new ClosestValueBST();
nums = new int[][] {{1, 2, 3, 4, 5, 6, 7}, {1, 2}, {2, 1} , {8, 1}};
double[] targets = {4.428571, 3.428571, 2147483647.0 , 6.0};
for (int i = 0; i < targets.length; i++) {
double target = targets[i];
TreeNode root = cbst.sortedArrayToBST(nums[i]);
System.out.println(cv.closestValue(root, target));
}
}
}
270. Closest Binary Search Tree Value
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Given a non-empty binary search tree and a target value, ...
- Given a non-empty binary search tree and a target value, ...
- Given a non-empty binary search tree and a target value, ...
- Solution1:二分查找 Recursive写法 思路:Time Complexity: O(logN) ...
- Given a non-empty binary search tree and a target value, ...