Preorder tree traversal function which returns an array of integers in C
<p>I try to write a function which return an array of integers containing the node values of the binary tree in preorder that is a node value must appear before the values of its left and right child.</p>

<ul>
<li><p>if root is NULL, return NULL</p></li>
<li><p>for every node left child comes before right child</p></li>
</ul>

<p>For example;</p>

<pre><code>int *a = preorder(bt1);
for (i=0; i&lt;3; i++)
    printf("%d ", a[i]);
&gt;2_1_3_
</code></pre>

<p>Here is my work, but it doesn't work, where could be the problem in my code?</p>

<pre><code>int* preorder(TreeNode *root) {
    int *a = malloc(sizeof(int)*50);
    int i=0;

    if(root == NULL)
        return NULL;
    else {
        if(root != NULL) {
            a[i] = root-&gt;val;
            i++;
            preorder(root-&gt;left);
            preorder(root-&gt;right);
            return a;
        }
    }
}
</code></pre>