由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问到G家题
相关主题
G的一道考题脸家电话面试面筋
请问leetcode的Binary Search Tree Iteratorcareercup上看的一道题
FB两次电面这个check whether a binary tree is a BST or not
关于leetcode上的一道题"Hacking a G Interview"怎么有这样低级错?
leetcode Runtime error : Flatten Binary Tree to Linked List问个算法题
回馈本版,新鲜店面,新题新气象请教这么一个题:BST maximum sum path
yahoo onsite这最小公共父母节点有bug吗?
请教个G题目大家帮忙看看 问题在哪啊?由preorder 来建 bst,为什么后面没
相关话题的讨论汇总
话题: curr话题: root话题: key话题: treenode话题: ceiling
进入JobHunting版参与讨论
1 (共1页)
S******n
发帖数: 132
1
A binary search tree is given. Find the ceiling value present in the BST of
a given key. eg-
8
3 12
2 6 10 15
4
key - 13 => 15
key - 4 =>6
key - 8 =>10
自己写了一个
TreeNode* find(TreeNode* root, int target)
{
if(!root) return NULL;
TreeNode * result = NULL;
TreeNode* curr = root;
while( curr )
{
if(target >= curr->val)
curr = curr->right;
else
{
result = curr;
curr = curr->left;
}
}
return result;
}
谁帮我看看对不对,感觉离开leetcode的onlinejudge自己不会测试了
p*****2
发帖数: 21240
2

of
感觉curr变量可以不需要

【在 S******n 的大作中提到】
: A binary search tree is given. Find the ceiling value present in the BST of
: a given key. eg-
: 8
: 3 12
: 2 6 10 15
: 4
: key - 13 => 15
: key - 4 =>6
: key - 8 =>10
: 自己写了一个

S******n
发帖数: 132
3
是不需要,这个题目是不是也可以转化
有duplicate的BST找lowerbound和upperbound?

【在 p*****2 的大作中提到】
:
: of
: 感觉curr变量可以不需要

l*****a
发帖数: 14598
4
为什么不是找previous/next node with inorder traverse

【在 S******n 的大作中提到】
: 是不需要,这个题目是不是也可以转化
: 有duplicate的BST找lowerbound和upperbound?

c******a
发帖数: 789
5
我觉得正确。
若遇到小/相等的node,那肯定不是解,往右走。遇到大了的点,就拼命往它左边走。
走到走不动。
c******a
发帖数: 789
6
你这就O(n)了。楼主那个O(lgN)

【在 l*****a 的大作中提到】
: 为什么不是找previous/next node with inorder traverse
r**h
发帖数: 1288
7
感觉target=4的时候会走到4呀

of

【在 S******n 的大作中提到】
: A binary search tree is given. Find the ceiling value present in the BST of
: a given key. eg-
: 8
: 3 12
: 2 6 10 15
: 4
: key - 13 => 15
: key - 4 =>6
: key - 8 =>10
: 自己写了一个

l*****a
发帖数: 14598
8
真的吗?

【在 c******a 的大作中提到】
: 你这就O(n)了。楼主那个O(lgN)
l*****a
发帖数: 14598
9
result里面记的不是6?

【在 r**h 的大作中提到】
: 感觉target=4的时候会走到4呀
:
: of

S******n
发帖数: 132
10
往左走才update,应该是6啊

【在 l*****a 的大作中提到】
: result里面记的不是6?
相关主题
回馈本版,新鲜店面,新题新气象脸家电话面试面筋
yahoo onsitecareercup上看的一道题
请教个G题目这个check whether a binary tree is a BST or not
进入JobHunting版参与讨论
l**d
发帖数: 746
11
这个还比较容易改,只要把等于的判断分开:等于的时候找右子树最左边的节点。
不过他这个算法只能从上往下找。如果输入6,应该返回root 8,好像就没办法了。
我觉得最直观的做法是in order traversal 变成一个sorted array,然后binary
search

【在 S******n 的大作中提到】
: 往左走才update,应该是6啊
S******n
发帖数: 132
12
如果是6的话会往右走,result并没有update还是会返回8

【在 l**d 的大作中提到】
: 这个还比较容易改,只要把等于的判断分开:等于的时候找右子树最左边的节点。
: 不过他这个算法只能从上往下找。如果输入6,应该返回root 8,好像就没办法了。
: 我觉得最直观的做法是in order traversal 变成一个sorted array,然后binary
: search

H****r
发帖数: 2801
13
key - 1
key - 16
应该返回啥???

of

【在 S******n 的大作中提到】
: A binary search tree is given. Find the ceiling value present in the BST of
: a given key. eg-
: 8
: 3 12
: 2 6 10 15
: 4
: key - 13 => 15
: key - 4 =>6
: key - 8 =>10
: 自己写了一个

l**d
发帖数: 746
14
实现了一下,的确是对的。往左走存下result设计不错。

【在 S******n 的大作中提到】
: 如果是6的话会往右走,result并没有update还是会返回8
l**d
发帖数: 746
15
1 返回 2阿
16我觉得可以返回Integer.MAXINT或者直接null

【在 H****r 的大作中提到】
: key - 1
: key - 16
: 应该返回啥???
:
: of

H****r
发帖数: 2801
16
这些edge case 需要确定吧,感觉1 返回2应该是不错, 16 可能直接throw error?

【在 l**d 的大作中提到】
: 1 返回 2阿
: 16我觉得可以返回Integer.MAXINT或者直接null

S******n
发帖数: 132
17
难道不是返回NULL吗?因为不存在

【在 H****r 的大作中提到】
: 这些edge case 需要确定吧,感觉1 返回2应该是不错, 16 可能直接throw error?
H****r
发帖数: 2801
18
不存在就返回NULL? 那小于最小的那个值也是吧...

【在 S******n 的大作中提到】
: 难道不是返回NULL吗?因为不存在
g**x
发帖数: 373
19
可以有O(lgN)的算法吗?
当BST为
1
2
3
4
...
如何O(lgN)呢?
在平均的情况下,in-order递归的方法也可以是O(lgN),在worst情况下, 是O(N)。

【在 c******a 的大作中提到】
: 你这就O(n)了。楼主那个O(lgN)
c******a
发帖数: 789
20
我不明白了,in-order怎么都要从最小node开始,一个个按顺序走到最后的。相当于一
个array去遍历,怎么lgN啊?
严密一点一个balanced BST,楼主解法我还是觉得是lgN。
相关主题
"Hacking a G Interview"怎么有这样低级错?这最小公共父母节点有bug吗?
问个算法题大家帮忙看看 问题在哪啊?由preorder 来建 bst,为什么后面没
请教这么一个题:BST maximum sum path问题在哪儿啊 kth Node of BST,大家帮忙
进入JobHunting版参与讨论
S******n
发帖数: 132
21
不平衡的bst怎么都是worst case O(n)
t*******2
发帖数: 182
22
也写了一个,欢迎大牛们挑错~~~
先assume全是正数,找不到就return -1了
---------------------------
public int ceiling (Node root, int key) {
if (root == null) { // if reached the bottom and no value is greater
than target
return -1; // ceiling is not found
}
if (root.val == key) { // if key is found, return key
return root.val;
}
if(root.val < key) { // if root is smaller than key
return ceiling(root.right, key); // ceiling can only be in
right sub tree
}
int ceil = ceiling(root.left, key); // else search in left sub tree
return ceil >= key? ceil : root.val; // if there is no such value in
left, root is the ceiling
}
g**x
发帖数: 373
23
when root->val < target, ceiling is definitely not in subtree of root.
when root->val > target, ceiling is definitely not in right subtree of root.
so no 遍历.

【在 c******a 的大作中提到】
: 我不明白了,in-order怎么都要从最小node开始,一个个按顺序走到最后的。相当于一
: 个array去遍历,怎么lgN啊?
: 严密一点一个balanced BST,楼主解法我还是觉得是lgN。

g**x
发帖数: 373
24
看不懂啊。自己写了个:
//
// return the pointer when a ceiling is found;
// otherwise, NULL;
//
TreeNode* find_ceiling(TreeNode* root, int target)
{
if (root==NULL) return NULL;
if (root->val > =target) {
TreeNode* temp = find_ceiling( root->left, target );
return (temp==NULL)? root: temp;
}
return find_ceiling( root->right, target );
}

greater

【在 t*******2 的大作中提到】
: 也写了一个,欢迎大牛们挑错~~~
: 先assume全是正数,找不到就return -1了
: ---------------------------
: public int ceiling (Node root, int key) {
: if (root == null) { // if reached the bottom and no value is greater
: than target
: return -1; // ceiling is not found
: }
: if (root.val == key) { // if key is found, return key
: return root.val;

c******a
发帖数: 789
25
哦!明白了,相当于binary search。那的确是lgN

root.

【在 g**x 的大作中提到】
: when root->val < target, ceiling is definitely not in subtree of root.
: when root->val > target, ceiling is definitely not in right subtree of root.
: so no 遍历.

c******a
发帖数: 789
26
忽然觉得自己好迟钝,居然去怀疑lolhaha T_T
x*****0
发帖数: 452
27
mark
1 (共1页)
进入JobHunting版参与讨论
相关主题
大家帮忙看看 问题在哪啊?由preorder 来建 bst,为什么后面没leetcode Runtime error : Flatten Binary Tree to Linked List
问题在哪儿啊 kth Node of BST,大家帮忙回馈本版,新鲜店面,新题新气象
A家面经求Offeryahoo onsite
leetcode上的sorted list to BST请教个G题目
G的一道考题脸家电话面试面筋
请问leetcode的Binary Search Tree Iteratorcareercup上看的一道题
FB两次电面这个check whether a binary tree is a BST or not
关于leetcode上的一道题"Hacking a G Interview"怎么有这样低级错?
相关话题的讨论汇总
话题: curr话题: root话题: key话题: treenode话题: ceiling