Create binary tree of fixed size
<p>I am trying to create a binary tree. Only thing I am given is the number of the nodes in the tree. The first thing popped in to my head is to use an index(<strong>BFS order</strong>) to keep the track of the number of total nodes, then use a recursive definition. Here is my pseudocode to do that.</p>

<pre><code>N = 10         //binary tree total node count
i = 0          //global integer
function()
    if i &gt; N
        return True

    create node i
    i = i + 1
    function(i) //left
    i = i + 1
    function(i) //right 
</code></pre>

<p>I have to use a global variable in this definition which makes me feel like maybe I am violating recursion rules. Is there a better way to do what I do, if this is the way, can it be improved ?</p>

<p><strong>Note:</strong> I am asking about the theoretical method, not the code.</p>

<p><strong>Edit:</strong> I just realized this method fails. I am open for suggestions.</p>

<p><strong>Clarification:</strong> The requirement for this tree is not to add an element to a depth, if the previous depth is not filled with nodes(all nodes have 2 children), pardon me for not mentioning this before, as for the stack I mentioned in the comments, it has nothing to do with the question, just the regular way of traversing trees iteratively.</p>