title: How to free a binary tree using C?
body: <p>I have written a binary search tree, it works fine but I'm not sure whether my program frees all the memories.</p>
  
  <p>here is my definition of the node of a tree</p>
  
  <pre><code>typedef struct node  {
      int val;
      struct node *left, *right;
  } nodeOfTree;
  </code></pre>
  
  <p>I write this function to output the result and free all the nodes, it seems that the answer is right but the memories are not freed.</p>
  
  <pre><code>void outputAndDestroyTree(nodeOfTree *root)  {
      if (!root) {return;}
      outputAndDestroyTree(root-&gt;left);
      printf("%d ", root-&gt;val);
      outputAndDestroyTree(root-&gt;right);
      free(root);      // I free this pointer, but after doing that, I can still access this  pointer in the main() function
  }
  </code></pre>
  
  <p>Is that mean I can not free a piece of memories in the recursive function? Thank you ~~~~~</p>
  
  <p>Update: Thank you all~</p>