avatar
google phone interview# JobHunting - 待字闺中
i*n
1
The interviewer mentioned if given a perfect balanced BST, if we have the
min node and continuously call the successor function n times.(n is the
number of nodes)So that we could have the in order traverse of the BST. Then
what's the complexity of this algorithms? He said the complexity is O(N),
could anybody help me with this? I am a little bit confused.
avatar
p*2
2

Then
有到parent的指针吗?

【在 i******n 的大作中提到】
: The interviewer mentioned if given a perfect balanced BST, if we have the
: min node and continuously call the successor function n times.(n is the
: number of nodes)So that we could have the in order traverse of the BST. Then
: what's the complexity of this algorithms? He said the complexity is O(N),
: could anybody help me with this? I am a little bit confused.

avatar
f*t
3
就是个中序遍历的iterator
avatar
i*n
4
shot! I got what you mean!

【在 f*******t 的大作中提到】
: 就是个中序遍历的iterator
avatar
z*9
5
因为是balanced,所以找下一个最多两步就可以了,所以是n的
avatar
b*c
6
PointerType Next()
{
if (m_stack.empty())
return NULL;
PointerType temp = m_stack.top();
m_stack.pop();
return FetchMin(temp->right);
}
PointerType FetchMin(PointerType pushPoint)
{
while (pushPoint)
{
m_stack.push(pushPoint);
pushPoint = pushPoint->left;
}
return m_stack.empty() ? NULL : m_stack.top();
}

Then

【在 i******n 的大作中提到】
: The interviewer mentioned if given a perfect balanced BST, if we have the
: min node and continuously call the successor function n times.(n is the
: number of nodes)So that we could have the in order traverse of the BST. Then
: what's the complexity of this algorithms? He said the complexity is O(N),
: could anybody help me with this? I am a little bit confused.

avatar
w*e
7
可以解释一下吗 谢谢

【在 f*******t 的大作中提到】
: 就是个中序遍历的iterator
avatar
f*t
8
http://en.wikipedia.org/wiki/Tree_traversal
用迭代的方式中序遍历,wiki上有代码。
把stack等状态封装在一个iterator里,就可以实现一步步的输出。时间复杂度是O(N)。

【在 w******e 的大作中提到】
: 可以解释一下吗 谢谢
avatar
N*8
9
那为什么必须是perfect balanced BST,任何的BST不是都可以?

)。

【在 f*******t 的大作中提到】
: http://en.wikipedia.org/wiki/Tree_traversal
: 用迭代的方式中序遍历,wiki上有代码。
: 把stack等状态封装在一个iterator里,就可以实现一步步的输出。时间复杂度是O(N)。

avatar
f*t
10
不是必须

【在 N*****8 的大作中提到】
: 那为什么必须是perfect balanced BST,任何的BST不是都可以?
:
: )。

avatar
N*8
11
我觉得面试官的思路不是这样的,他说的是如果给个perfect balanced BST,然后给你
个min node,然后呼叫n次的findSuccesor() method,那么你能得到一个sorted
traversal(这个非常trivial)。但是他说时间复杂度是O(N),这个我想不通,我推算了
一下应该要O(nlogn),因为worst case findSuccesor()需要O(logn)。

【在 f*******t 的大作中提到】
: 不是必须
avatar
z*w
12
建议去看一下平摊分析,O(n*logn)是上界,担不是紧确的
avatar
N*8
13
"平摊分析"amortized analysis?
能给一个数学的推算公式吗?

【在 z******w 的大作中提到】
: 建议去看一下平摊分析,O(n*logn)是上界,担不是紧确的
avatar
H*e
14
窝也觉得出题者的意思没那么复杂
就是指有parent指针,然后successor常数操作。(worst非常数)
就是完全csrl上算法

【在 p*****2 的大作中提到】
:
: Then
: 有到parent的指针吗?

avatar
N*8
15
再就算有parent指针的情况下,CLRS说的是O(h) where h=logn in perfect balanced
BST。

【在 H***e 的大作中提到】
: 窝也觉得出题者的意思没那么复杂
: 就是指有parent指针,然后successor常数操作。(worst非常数)
: 就是完全csrl上算法

avatar
N*8
16
OK,看到了,CLRS的习题。答案的确是O(N),思考中。

【在 H***e 的大作中提到】
: 窝也觉得出题者的意思没那么复杂
: 就是指有parent指针,然后successor常数操作。(worst非常数)
: 就是完全csrl上算法

avatar
H*e
17

是的, 最坏是O(lgn)
但是总和是O(n)锕,就是泥打印出来整个inorder traversal,每条边被经过两次
就是inorder traversal的路径

balanced

【在 N*****8 的大作中提到】
: 再就算有parent指针的情况下,CLRS说的是O(h) where h=logn in perfect balanced
: BST。

avatar
N*8
18
多谢大牛。

【在 H***e 的大作中提到】
: 蒽
: 是的, 最坏是O(lgn)
: 但是总和是O(n)锕,就是泥打印出来整个inorder traversal,每条边被经过两次
: 就是inorder traversal的路径
:
: balanced

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。