title: How to delete all the nodes of BST for reuse in c++
body: <p>I couldn't delete all the nodes of BST for reuse.For the first set of inputs my code works properly but fails later input.
  I googled a lot and couldn't find any solution related my problem.
  For clarification I am pasting my full source code.
  I found this kind of question previously asked here
  <a href="https://stackoverflow.com/questions/12872387/how-to-delete-all-nodes-of-a-binary-search-tree">How to delete all nodes of a Binary Search Tree</a>
  I don't know python very much.
  For this a asked here again about this topic.</p>
  
  <pre><code>    #include&lt;bits/stdc++.h&gt;
      using namespace std;
      int size,c=0;
      struct BstNode {
        int data;
        BstNode* left;
        BstNode* right;
      };
  
      BstNode* insert(BstNode* root,int data) {
        if(root==NULL) {
          root=new BstNode();
          root-&gt;data=data;
          root-&gt;left=root-&gt;right=NULL;
        }
        else if(data&lt;root-&gt;data) {
          root-&gt;left=insert(root-&gt;left,data);
        }
        else {
          root-&gt;right=insert(root-&gt;right,data);
        }
        return root;
      }
  
      void preorderTraversal(BstNode* root) {
        if(root!=NULL) {
  
          c++;
          if(c==size) cout&lt;&lt;root-&gt;data&lt;&lt;endl;
          else cout&lt;&lt;root-&gt;data&lt;&lt;" ";
          preorderTraversal(root-&gt;left);
          preorderTraversal(root-&gt;right);
        }
      }
  
      void inorderTraversal(BstNode* root) {
        if(root!=NULL) { 
          inorderTraversal(root-&gt;left);
          c++;
          if(c==size) cout&lt;&lt;root-&gt;data&lt;&lt;endl;
          else cout&lt;&lt;root-&gt;data&lt;&lt;" ";
          inorderTraversal(root-&gt;right);
        }
      }
  
      void postorderTraversal(BstNode* root) {
        if(root!=NULL) {
          postorderTraversal(root-&gt;left);
          postorderTraversal(root-&gt;right);
          c++;
          if(c==size) cout&lt;&lt;root-&gt;data&lt;&lt;endl;
          else cout&lt;&lt;root-&gt;data&lt;&lt;" ";
        }
      }
  
      void deletepostorderTraversal(BstNode* root) {
        if(root!=NULL) {
          deletepostorderTraversal(root-&gt;left);
          deletepostorderTraversal(root-&gt;right);
          delete root;
        }
      }
  
      int main() {
        int test_case;
        while(cin&gt;&gt;test_case) {
          cin&gt;&gt;size;
          BstNode* root=NULL; //create the root
          for(int i=0;i&lt;size;i++) {
            int data;cin&gt;&gt;data;
            root=insert(root,data);
          }   
          c=0;
          cout&lt;&lt;"Pre.: ";
          preorderTraversal(root);
          cout&lt;&lt;"In..: ";
          c=0;
          inorderTraversal(root);
          cout&lt;&lt;"Post: ";
          c=0;
          postorderTraversal(root);
          cout&lt;&lt;endl;
          deletepostorderTraversal(root); //delete all the nodes 
          //i can't reuse this code again.
         // root=NULL;
        //  if(root==NULL) cout&lt;&lt;"Deleted"&lt;&lt;endl;
        }
      }
  </code></pre>