Write a recursive range function that given a binary search tree, two integers k1 and k2 such that k1 ≤ k2 and a vector v, inserts in v all the keys in ascending order organized in the tree such that k1 ≤ k ≤ k2 . The function returns the size of the vector v.
This is my solution:
typedef struct node {
int key;
struct node * left;
struct node * right;
struct node * parent;
} * Node;
void rangeRic(Node u, int k1, int k2, int v[], int * pindex) {
if (u == NULL) {
return;
}
rangeRic(u->left,k1,k2,v,pindex);
if (u->key >= k1 && u->key <= k2) {
v[*pindex] = u->key;
*pindex += 1;
}
rangeRic(u->right,k1,k2,v,pindex);
}
int range(Node u, int k1, int k2, int v[]) {
int i = 0;
rangeRic(u,k1,k2,v,&i);
return i;
}
My problem is that the exercise description also states the following:
You cannot use any auxiliary function and the vector v has enough space to contain all the elements that satisfy the condition.
and this last statement invalidates my solution.
How to compile:
gcc -std=gnu89 -pedantic -Wall -o main main.c binarySearchTree.c
Could you guys help me with this ?