Tree-traversal without recursion I wrote the following code for printing all the paths to the leaf nodes in a tree (NOT a binary tree ) without using recursion. I have used a stack and performed dfs on the tree. Whenever I reach a leaf node I pop all the elements in the stack right till the root so that the function starts over again from the root and prints a path to another leaf.

The tree that I have assumed in the program is as follows


                              0
                      1            2       3
              4   5      6              7     8



Explanation:


1, 2 and 3 are children of 0; 
4, 5, 6 are children of 1; 
7 and 8 are children of 3.


My code

//print paths in a tree without recursion
#include&lt;iostream&gt;
#include&lt;vector&gt;
#include&lt;stack&gt;
using namespace std;
struct node                         
{
       int data;
       vector&lt;node*&gt;children;
};
int visited[100];
stack &lt;node*&gt; st;
int count=0;
int n;      
void printpath(node * root)                               //dfs starts here
{
     st.push(root);
     while(visited[0]==0)
     {
         node* v=st.top();
         if(visited[v-&gt;data]!=0)                 // leaf nodes or nodes whose all children 
            st.pop();                            // are visited are only popped from the stack
         if(visited[v-&gt;data])
            continue;
         cout&lt;&lt;v-&gt;data&lt;&lt;" ";
         int flag=0;
         for(int i=0;i&lt;v-&gt;children.size();i++)      /*check if any of the children have not been visited*/
         {
                 if(visited[v-&gt;children[i]-&gt;data]==0)
                 {
                      flag=flag||1;
                      st.push(v-&gt;children[i]);
                 }
                 else
                       flag=flag||0;
         }

         /*if this is the leaf node then mark it visited and
         along with other intermediate nodes whose all the   
         children have been visited.this is continued till we
         reach root which has the value 0 in its data*/

         if(flag==0)
         {                                      
                 visited[v-&gt;data]=1;                       
                  while(v-&gt;data!=0)                         
                  {
                     v=st.top();
                     int flag=0;
                     for(int i=0;i&lt;v-&gt;children.size();i++)
                     {
                        if(visited[v-&gt;children[i]-&gt;data]==0)
                          flag=flag||1;
                       else
                         flag=flag||0;
                     }
                        if(flag==0 &amp;&amp; v-&gt;children.size()!=0)
                          visited[v-&gt;data]=1;
                        if(v-&gt;data!=0)
                          st.pop();
                  }
                  cout&lt;&lt;endl;
         }
     }
}
main()
{
   n=8;
   for(int i=0;i&lt;9;i++)
       visited[i]=0;
   node*root = new node();                         //tree construction
   root-&gt;data=0;
   node* d = new node();
   d-&gt;data=1;
   node* m = new node();
   m-&gt;data=2;
   node* n = new node();
   n-&gt;data=3;
   root-&gt;children.push_back(d);
   root-&gt;children.push_back(m);
   root-&gt;children.push_back(n);
   node* x = new node();
   x-&gt;data=4;
   node* y = new node();
   y-&gt;data=5;
   node* z = new node();
   z-&gt;data=6;
   d-&gt;children.push_back(x);
   d-&gt;children.push_back(y);
   d-&gt;children.push_back(z);
   node* o = new node();
   o-&gt;data=7;
   node* p = new node();
   p-&gt;data=8;
   n-&gt;children.push_back(o);
   n-&gt;children.push_back(p);
   printpath(root);
}


My algorithm seems really inefficient. Can anyone suggest some ways to improve it?

The output is:


0 3 8 
0 3 7 
0 2 
0 1 6 
0 1 5 
0 1 4
In order that things appear in the code:

using namespace std;


This is unnecessary and generally bad practice.  I would ask about it if I saw it in an interview question, although it is not harmful in this case.

struct node
{
    node(int d) : data(d) {}
    int data;
    boost::ptr_vector&lt;node&gt; children;
};


Indentation is generally done in powers of two, and should in any case be consistent.  I'm switching to four-space indent everywhere, as you don't seem to be following any style.

This is also a great chance to use pointer containers.  Each node owns all of its children, so use something that will enforce that.  (In some cases, an std::vector of std::shared_ptrs would make more sense.)

You do not need any of the globals you've defined, and are definitely bad practice.  You should also strive to be more const-correct.

void printpath(node const* root)
{
    std::stack&lt;node const*&gt; path;
    std::set&lt;node const*&gt; visited;
    path.push(root);
    while (visited.find(root) != visited.end())
    {
        node const* v = path.top();
        if (visited.find(v) != visited.end())
        {
            path.pop();
            continue;
        }
        std::cout &lt;&lt; v-&gt;data &lt;&lt; " ";
        bool has_unvisited_child = false; // Use bools for booleans.
        // See standard comments on iterators vs indices and preincrement
        // vs postincrement.
        for (auto it = v-&gt;children.cbegin(); it != v-&gt;children.cend(); ++it)
        {
            if (visited.find(&amp;*it) != visited.end())
            {
                 has_unvisited_child = true;
                 st.push(&amp;*it);
            }
        }

        // Don't rely on the root node having value 0 in its data.
        // You were given a pointer to it, use that.  Your algorithm
        // should not depend on the data stored in the tree.

        // Avoids some nesting.
        if (!has_unvisited_child)
            continue;

        visited.insert(v);                       
        while(v != root)                         
        {
            v = st.top();
            // Avoid shadowing.
            bool unvisited_child = 0;
            // Splitting this off into a function may be meaningful,
            // or using some standard algorithm such as std::any_of
            for (auto it = v-&gt;children.cbegin(); it != v-&gt;children.cend(); ++it)
                unvisited_child |= visited.find(&amp;*it) != visited.end();
            if (!unvisited_child &amp;&amp; !v-&gt;children.empty())
                visited.insert(v);
            if (v != root)
                st.pop();
        }
        std::cout&lt;&lt;endl;
    }
}

// main must return int
int main()
{
    node* root = new node(0);
    node* d = new node(1);
    node* m = new node(2);
    node* n = new node(3);
    root-&gt;children.push_back(d);
    root-&gt;children.push_back(m);
    root-&gt;children.push_back(n);
    node* x = new node(4);
    node* y = new node(5);
    node* z = new node(6);
    d-&gt;children.push_back(x);
    d-&gt;children.push_back(y);
    d-&gt;children.push_back(z);
    node* o = new node(7);
    node* p = new node(8);
    n-&gt;children.push_back(o);
    n-&gt;children.push_back(p);
    printpath(root);
}


Now for the performance part:  My advice on that end is to not bother with the stack of next nodes and just keep track of the path:

void print_path(node const* root)
{
    std::vector&lt;node const*&gt; path{root};
    std::set&lt;node const*&gt; visited;
    while (!path.empty())
    {
        node const* n = path.back();
        if (std::all_of(n-&gt;children.cbegin(), n-&gt;children.cend(), [&amp;](node const&amp; a) { return visited.find(&amp;a) != visited.end(); }))
        {
            if (n-&gt;children.empty()) {
                for (auto it = path.cbegin(); it != path.cend(); ++it)
                {
                    if (it != path.cbegin())
                        std::cout &lt;&lt; ", ";
                    std::cout &lt;&lt; (*it)-&gt;data;
                }
                std::cout &lt;&lt; '\n';
            }
            visited.insert(n);
            path.pop_back();
        } else {
            path.push_back(&amp;*std::find_if(n-&gt;children.cbegin(), n-&gt;children.cend(), [&amp;](node const&amp; a) { return visited.find(&amp;a) == visited.end(); }));
        }
    }
}


I am not sure which is more efficient.