Iteration to recursive function In my first iterative algorithm, I do this

for (auto &amp;widget : controls.getWidgets())
{
    if (!widget-&gt;visible) continue;

    widget-&gt;draw();

    for (auto &amp;widget_component : widget-&gt;components)
    {
        if (!widget_component-&gt;visible) continue;

        widget_component-&gt;draw();

        for (auto &amp;ww : widget_component-&gt;components)
        {
            if (!ww-&gt;visible) continue;

            ww-&gt;draw();
        }
    }
}


Now, I see the flaw in this algorithm because of the repetition of code.

I try to write a recursive function call to handle this thing.

void swcApplication::recursiveDisplay(swcWidget *next)
{
    if (next == nullptr) return;

    if ((next-&gt;parent != nullptr) &amp;&amp;
        !next-&gt;parent-&gt;visible) return;

    if (next-&gt;visible)
        next-&gt;draw();

    for (auto &amp;i : next-&gt;components)
    {
        recursiveDisplay(i);
    }
}

void display()
{
    for (auto &amp;widget : controls.getWidgets())
    {
        recursiveDisplay(widget);
    }
}


Display first the parent before all other children, if parent is not visible, then don't draw its children

Is the above phrase satisfy by this algorithm (As far as I can see, it is)? Is this optimal? I don't know what drawbacks might occur here because I just write in a few seconds.

If you didn't find anything wrong here, now, how can I put it back in iterative way? I know iteration is better.

Update

I didn't use any z attribute here and instead I go for painter's algorithm.

The flaw I am referring in my iteration version is that, it is limited to draw widgets depends on how deep I will code for iteration.
You can modify the recursive code to use an explicit stack:

#include &lt;stack&gt;

void display_widgets(control&amp; controls)
{
    std::stack&lt;widget*&gt; widgets;
    for(auto &amp;widget : controls.getWidgets()) {
        widgets.push(widget);
    }

    while(!widgets.empty()) {
        widget* w = widgets.top();
        widgets.pop();
        if(!widget-&gt;visible) {
            continue;
        }
        widget-&gt;draw();
        for(auto&amp; component : widget-&gt;components) {
            widgets.push(component);
        }
    }
}



  I know iteration is better.


This is highly dependent on language and usage. It is "better" in that recursion can use O(n) stack space (although not always - tail call optimization gets rid of this limitation), and won't blow your stack. However, in this case, assuming the recursive algorithm actually did what you wanted, it would be better and easier. 

You can always turn a recursive solution into an iterative one by replacing the call stack with an actual stack of your own, as above. For very deeply nested recursive calls, this may save you from stack overflows, however, it is more fiddly to code, and in this case, doesn't gain you anything. 

Having written the code above, my advice would be to stick with the recursive solution.