title: Convert recursive binary tree traversal to iterative
body: <p>I was asked to write the iterative version, but I wrote the recursive version i.e.</p>
  
  <pre><code>void inorderTraverse(BinaryTree root)
  {
      if(root==NULL)
          printf("%d",root-&gt;id);
      else
      {
          inorderTraverse(root-&gt;left);
          printf("%d",root-&gt;id);
          inorderTraverse(root-&gt;right);
      }
  }
  </code></pre>
  
  <p>I'm not looking for the code, I want to understand how this can be done. Had it been just the last recursive call, I would have done</p>
  
  <pre><code>void inorderTraverse(BinaryTree root)
  {
      while(root!=NULL)
      {
          printf("%d",root-&gt;id);
          root=root-&gt;right;
      }
  }
  </code></pre>
  
  <p>But <strong>how do I convert to an iterative program when there are two recursive calls?</strong></p>
  
  <p>Here are the type definitions.</p>
  
  <pre><code>struct element{
      struct element* parent;
      int id;
      char* name;
      struct element* left;
      struct element* right;
  };
  typedef element* BinaryTree;
  </code></pre>
  
  <p>This is what I thought of, am I on the right track?</p>
  
  <pre><code>temp=root;
  while(1)
  {
      while(temp!=NULL)
      {
       push(s,temp);
       temp=temp-&gt;left;
       continue;
      }
  
      temp=pop(s);
      if(temp==NULL)
      return;
      printf("%d\t",temp-&gt;data);
      temp=temp-&gt;right;
  }
  </code></pre>