/*
* 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?
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來炒辉,“玉大人豪墅,你說我怎么就攤上這事∏埽” “怎么了偶器?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長缝裤。 經(jīng)常有香客問我屏轰,道長,這世上最難降的妖魔是什么憋飞? 我笑而不...
- 正文 為了忘掉前任霎苗,我火速辦了婚禮,結(jié)果婚禮上榛做,老公的妹妹穿的比我還像新娘唁盏。我一直安慰自己内狸,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布厘擂。 她就那樣靜靜地躺著昆淡,像睡著了一般。 火紅的嫁衣襯著肌膚如雪刽严。 梳的紋絲不亂的頭發(fā)上昂灵,一...
- 文/蒼蘭香墨 我猛地睜開眼把还,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了茸俭?” 一聲冷哼從身側(cè)響起吊履,我...
- 正文 年R本政府宣布咒彤,位于F島的核電站疆柔,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏镶柱。R本人自食惡果不足惜旷档,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望奸例。 院中可真熱鬧彬犯,春花似錦向楼、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至宋列,卻和暖如春昭抒,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背炼杖。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 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, ...