Question: I have this function that finds the preorder of a binary tree. I'm a bit unsure how I can edit this to store the traversal instead of printing it. I want to store it in an array possibly so I can compare it to another traversal, but creating an array within this function will be an issue since I implemented it recursively. Any ideas? 
I was thinking of passing it an empty array, but how I can't seem to picture how I'd increment through the array due to the function being recursive.

```
void preorder(node *node)
{
    if(node == NULL)
         return;

    printf("%d", node->data);
    preorder(node->left);
    preorder(node->right);
}

```

Answer: 
You are on the right track. Yes, pass in an array that is initially empty. Also pass an index initialized to 0 to keep track of how much of the array you have filled in. `*index` represents the next array index available for filling in data. You increment the index only when you fill data in the array. The recursive cases will be handled naturally. Each invocation of `inorder` will increment the index by 1.

```
void inorder(node *node, int *array, int *index)
{
    if (node == NULL)
        return;

    inorder(node->left, array, index);

    array[(*index)++] = node->data;

    inorder(node->right, array, index);
}

```
You just have to modify your preorder traversal code a bit and keep storing the values in an array using the `index` variable.

```
int arr[10]; // Change it to the number of nodes in your tree
int index = 0;
void store(struct node *root)
{

    if (root != NULL)
    {

        printf("%d \n", root->key);
        arr[index++] = root->key;
        store(root->left);
        store(root->right);
    }
}

```