Nov 13, 2009

Known binary search tree and two values, find the number of nodes in the binary search tree, where the value of node is between two values

int foo(tree* node, int min, int max, int& count)
{
if(!root) return;
if(root->val>min && root->val < max)
{
count++;
foo(node->left,min,max,count);
foo(node->right,min,max,count);
}
else if(root->val>max) foo(node->left,min,max,count);
else if(root->val < min) foo(node->right,min,max,count);
}

No comments:

Post a Comment