title: Deep copy of binary tree
body: <p>I have this tree with different types of nodes that I need to do a deep copy on. The hierarchy looks something like this:</p>
  
  <pre><code>class AllNodes
  {
      //this is a purely virtual base class
  };
  class TreeNode : public AllNodes
  {
      AllNodes *rChild, *lChild;
  };
  class LeefNode : public AllNodes
  {
      int value;
  };
  </code></pre>
  
  <p>The problem is that when I want to do a deep copy of the entire tree, I don't know what nodes will have children and what nodes will have values. I've tried this, but it wont work (for obvious reasons):</p>
  
  <pre><code>void AllNodes::deepCopy(AllNodes* &amp;copied, AllNodes* o)
  {
      if(o-&gt;rChild == nullptr)
          copied-&gt;rChild = nullptr;
      else
      {
          copied-&gt;rChild = o-&gt;rChild;   
          deepCopy(copied-&gt;rchild, o-&gt;rChild);
      }
  
      if(o-&gt;lChild == nullptr)
          copied-&gt;lChild = nullptr;
      else
      {
          copied-&gt;lChild = o-&gt;lChild;
          deepCopy(copied-&gt;lChild, o-&gt;lChild);
      }
  }
  </code></pre>
  
  <p>Does anyone have some ideas of how to accomplish this?</p>