## Groupon Interview Question

SDE1s**Country:**India

I agree with your answer, but you are traversing whole tree. Why not skip a part of tree if it is either less than a OR greater than b ?

```
void countInsideRange(node *root, int min, int max, int* count)
{
if (root == NULL)
return;
if( (root->key < min) || (root->key > max) )
return;
if((root->key > min) && (root->key < max))
*count = *count + 1;
countInsideRange(root->left, min, max, count);
countInsideRange(root->right, min, max, count);
}
```

```
void countInsideRange(node *root, int min, int max, int* count)
{
if (root == NULL)
return;
if( root->key < min ) {
countInsideRange(root->right, min, max, count);
}else if( root->key > max ) {
countInsideRange(root->left, min, max, count);
}else if( (root->key >= min) && (root->key <= max) ) {
*count = *count + 1;
countInsideRange(root->left, min, max, count);
countInsideRange(root->right, min, max, count);
}else {
return;
}
}
```

Following is one of the many ways to do this.

```
Take the inorder traversal. Since it is sorted you easily check the number of nodes having values between a and b.
O(n) where n is total number of nodes.
```

You can optimize the above if you count the node during traversal itself and reject the left child of nodes having values less than a and right child of nodes having values greater than b.

```
/**
* count the no of nodes in the subtree rooted at root and containing n1 and n2.
*
*/
int countNodes(struct node* root)
{
int count=0;
if(!root)
return count;
else
{
count=countNode(root->left);
count=countNode(root->right);
count++;
}
return count;
}
int LCA(struct node* root, int n1, int n2)
{
if(root == NULL || root->data == n1 || root->data == n2)
return -1;
if((root->right != NULL) &&
(root->right->data == n1 || root->right->data == n2))
return root->data;
if((root->left != NULL) &&
(root->left->data == n1 || root->left->data == n2))
return root->data;
if(root->data > n1 && root->data < n2)
return countNodes(root->data);
if(root->data > n1 && root->data > n2)
return LCA(root->left, n1, n2);
if(root->data < n1 && root->data < n2)
return LCA(root->right, n1, n2);
```

}

```
public static int countNodesInRange(Integer a, Integer b, TreeNode<Integer> root) {
if(root == null) {
return 0;
}
if(root.value > a && root.value < b) {
return 1 + countNodesInRange(a, b, root.left) + countNodesInRange(a, b, root.right);
} else if(root.value >= b) {
return countNodesInRange(a, b, root.left);
} else if(root.value <= a) {
return countNodesInRange(a, b, root.right);
} else {
return 0;
}
}
```

```
public static int countNodesInRange(Integer a, Integer b, TreeNode<Integer> root) {
if(root == null) {
return 0;
}
if(root.value > a && root.value < b) {
return 1 + countNodesInRange(a, b, root.left) + countNodesInRange(a, b, root.right);
} else if(root.value >= b) {
return countNodesInRange(a, b, root.left);
} else if(root.value <= a) {
return countNodesInRange(a, b, root.right);
} else {
return 0;
}
}
```

It can be easily done using recursion.

The answer will be in integer count value.

- aasshishh April 04, 2013