由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 给个float BST, 写个search(float target)的算法找出离target最近的数。
相关主题
recovery BST 不考虑相同值的情况么?请问二叉搜索树如何找到两个点的最近祖先?
面试题麻烦谁贴一个bug free的BST next node
BST面试题find the k-th maximum node in a binary search tree 有疑问
求教:binary search tree中找第i大的数问题在哪儿啊 kth Node of BST,大家帮忙
一道二叉树的老题发现一个很恶心的基础问题
想到一道老题纽约小公司skype电面一题
一个老题binary tree找 lowest common ancestor 的code (请教Find the node with given value in binary tree in in-order
判断BT是否BST?一个很简单的问题,有没有快一点的算法?
相关话题的讨论汇总
话题: float话题: target话题: itr话题: root话题: rt
进入JobHunting版参与讨论
1 (共1页)
s*i
发帖数: 388
1
给个float BST, 写个search(float target)的算法找出离target最近的数。
给个solution?
b******n
发帖数: 4509
2
float minDist(node *root, float target, float m = FLOAT_MAX) {
if (!root)
return m;
if (root->val == target)
retrun 0;
if (root->val > target)
return min(m, minDist(root->left, target, root->val - target);
if (root->val < target)
return min(m, minDist(root->right, target, target - root->val);
}

【在 s*i 的大作中提到】
: 给个float BST, 写个search(float target)的算法找出离target最近的数。
: 给个solution?

f***g
发帖数: 214
3
请指教:
float closest(Node* root, float target) {
Node* itr = root;
float rt_1;
float rt_2;
while( itr != 0 ) {
if( itr->data < target ) {
rt_1 = itr->data;
itr = itr->right;
} else {
rt_2 = itr->data;
itr = itr->left
}
}
if( target-rt_1 < rt_2-target ) {
return rt_1;
} else {
return rt_2;
}
}
s*i
发帖数: 388
4
果然牛人神出鬼没!!!
应该是对的。

【在 b******n 的大作中提到】
: float minDist(node *root, float target, float m = FLOAT_MAX) {
: if (!root)
: return m;
: if (root->val == target)
: retrun 0;
: if (root->val > target)
: return min(m, minDist(root->left, target, root->val - target);
: if (root->val < target)
: return min(m, minDist(root->right, target, target - root->val);
: }

r********r
发帖数: 2912
5
but this solution only returns a minimal distance, not the pointer to the
node

【在 s*i 的大作中提到】
: 果然牛人神出鬼没!!!
: 应该是对的。

g*********s
发帖数: 1782
6
why float? isn't int equivalent?

【在 s*i 的大作中提到】
: 给个float BST, 写个search(float target)的算法找出离target最近的数。
: 给个solution?

s*i
发帖数: 388
7
int also works.

【在 g*********s 的大作中提到】
: why float? isn't int equivalent?
1 (共1页)
进入JobHunting版参与讨论
相关主题
一个很简单的问题,有没有快一点的算法?一道二叉树的老题
Google Front-end Software Engineer Phone Interview想到一道老题
bloomberg onsite题一个老题binary tree找 lowest common ancestor 的code (请教
How to find the kth biggest number in a BST判断BT是否BST?
recovery BST 不考虑相同值的情况么?请问二叉搜索树如何找到两个点的最近祖先?
面试题麻烦谁贴一个bug free的BST next node
BST面试题find the k-th maximum node in a binary search tree 有疑问
求教:binary search tree中找第i大的数问题在哪儿啊 kth Node of BST,大家帮忙
相关话题的讨论汇总
话题: float话题: target话题: itr话题: root话题: rt