title: C - binary tree: can't return correct number of pruned nodes
body: <p>I need to prune a binary tree past a certain level, l and need to return the number of pruned nodes.</p>
  
  <p>Here's what I got:</p>
  
  <pre><code>#include "abin.h"
  
  int freeAB (ABin a) {
      int count = 0;
      if(a == NULL) return count;
      count = count + freeAB(a-&gt;esq);
      count = count + freeAB(a-&gt;dir);
      free(a);
      count++;
      return count;
  }
  
  
  int pruneAB (ABin *a, int l) {
  
      int count = 0;
      if(l == 0){
          count = count + freeAB((*a)-&gt;esq);
          count = count + freeAB((*a)-&gt;dir);
          (*a) = NULL;
      }
      else{
          count = count + pruneAB(&amp;((*a)-&gt;esq), l-1);
          count = count + pruneAB(&amp;((*a)-&gt;dir), l-1);
      }
      return count;
   }
  </code></pre>
  
  <p><strong>ABIN.H:</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct lligada {
      int valor;
      struct lligada *prox;
  } *LInt;
  
  typedef struct nodo {
      int valor;
      struct nodo *esq, *dir;
  } *ABin;
  
  int pruneAB (ABin *a, int l);     
  </code></pre>
  
  <p><strong>This is the output of what I should have got and what I got:</strong></p>
  
  <pre><code>Input: (depth=2)
                 8
         4              12
     2       6      10      14
   1   3   5   7   9  11  13  15
  
  Output:
  [expected] res=12
     8
   4  12
  
  [obtained] res=8
     8
   4  12
  
  0/10 correct answers
  </code></pre>
  
  <p>Interestingly, if I create something like int r = 0; and do r++; every time the if(l == 0) statement is true, and then do a print statement, it prints r 4 times.</p>
  
  <p>If I added 4 to the final count I would get the correct answer. I assume that I should then add to count the number of times the if(l == 0) is true.</p>
  
  <p>(I can't do it. If I do count++ I get segmentation fault)</p>
  
  <p>How would you do it? Thanks.</p>
  
  <p><a href="https://codeboard.io/projects/16275" rel="nofollow">https://codeboard.io/projects/16275</a></p>