Redian新闻
>
纽约小公司skype电面一题
avatar
纽约小公司skype电面一题# JobHunting - 待字闺中
c*C
1
Given a binary search tree, find the total number of integers
in between min and max.
30
/
25 40
/
15 28 50
i.e.: count_nodes(..., 20, 50) --> 5
class BST(object):
"""
A Binary search tree.
"""
def __init__(self, value, left, right):
self.value = value
self.left = left
self.right = right
def count_nodes(self, min_, max_):
def count_nodes_(self, count):
if self is not None:
# need to traverse both the left child and the right child
if min_ <= self.value <= max_:
count += 1
count = count_nodes_(self.left, count)
count = count_nodes_(self.right, count)
# only need to traverse the right child
elif self.value < min_:
count = count_nodes_(self.right, count)
# only need to traverse the left child
else:
count = count_nodes_(self.left, count)
return count
return count_nodes_(self, 0)
root = BST(35,
BST(25,
BST(18,
BST(7, None, None),
BST(20, None, None)),
BST(32,
None,
BST(34, None, None))),
BST(43,
BST(40, None, None),
BST(60,
BST(50, None, None),
BST(64, None, None))))
print(root.count_nodes(20, 50))
avatar
m*n
2
谢谢。

【在 c***C 的大作中提到】
: Given a binary search tree, find the total number of integers
: in between min and max.
: 30
: /
: 25 40
: /
: 15 28 50
: i.e.: count_nodes(..., 20, 50) --> 5
: class BST(object):
: """

avatar
D*d
3
Solution: Tree recursion.
// root != NULL.
int GetNodeNumberBetween(node*root,int min, int max){
int num = root->val >= min && root->val <= max;
if(root->left && root->val >= min) num += GetNodeNumberBetween(root-Left,
min, max);
if(root->right && root->val <= max) num += GetNodeNumberBetween(root-Left,
min,
max);
return num;
}
avatar
s*u
4
有笔误,第二个if语句后面是root->right 呵呵

Left,

【在 D**********d 的大作中提到】
: Solution: Tree recursion.
: // root != NULL.
: int GetNodeNumberBetween(node*root,int min, int max){
: int num = root->val >= min && root->val <= max;
: if(root->left && root->val >= min) num += GetNodeNumberBetween(root-Left,
: min, max);
: if(root->right && root->val <= max) num += GetNodeNumberBetween(root-Left,
: min,
: max);
: return num;

avatar
c*p
5
Mark
avatar
s*s
6
难道不是 inorder tree transversal?
avatar
s*w
7
就是每个 node 加个 rank 最简单啊, 不知道你这个题目里面的 bst 是自己设计还
是给定的?

【在 c***C 的大作中提到】
: Given a binary search tree, find the total number of integers
: in between min and max.
: 30
: /
: 25 40
: /
: 15 28 50
: i.e.: count_nodes(..., 20, 50) --> 5
: class BST(object):
: """

avatar
s*w
8
这个很牛叉

Left,

【在 D**********d 的大作中提到】
: Solution: Tree recursion.
: // root != NULL.
: int GetNodeNumberBetween(node*root,int min, int max){
: int num = root->val >= min && root->val <= max;
: if(root->left && root->val >= min) num += GetNodeNumberBetween(root-Left,
: min, max);
: if(root->right && root->val <= max) num += GetNodeNumberBetween(root-Left,
: min,
: max);
: return num;

avatar
s*u
9
是3楼写的那个方法,是preorder。你是不是理解成,min是节点代表的值的最小值?,
我一开始也这么以为,后来一看不对。。min和max是给定的,否则例子不对。

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