title: create an array with the values of the nodes at a level
body: <p>I have to create an array that contains the values of nodes in a level passed as parameter. The function <code>createArray</code> has to return an array containing the values of the nodes at the level passed as parameter. My code is:</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  struct node {
      int data;
      struct node* left;
      struct node* right;
  };
  
  struct node* newNode(int data) {
      struct node* node = (struct node*)malloc(sizeof(struct node));
      node-&gt;data = data;
      node-&gt;left = NULL;
      node-&gt;right = NULL;
  
      return node;
  }
  
  int numNodesLevel(struct node* root, int level) {
      if (root == NULL) {
          return 0;
      }
      else {
          if (level == 0) {
              return 1;
          }
          return numNodesLevel(root-&gt;left, level - 1) + numNodesLevel(root-&gt;right, level - 1);
      }
  }
  
  int max(int a, int b) {
      if(a &gt; b) {
          return a;
      }
      else {
          return b;
      }
  }
  
  int height(struct node* root) {
      if (root == NULL) {
          return -1;
      }
      else {
          if (root-&gt;left == NULL &amp;&amp; root-&gt;right == NULL) {
              return 0;
          }
          return max(height(root-&gt;left), height(root-&gt;right)) + 1;
      }
  }
  
  int maxNodesLevel(struct node* root) {
      if (root == NULL) {
          return 0;
      }
      else {
          int max = 0;
          int h = height(root);
          int i = 0;
          for (i = 0; i &lt;= h; i++) {
              if (numNodesLevel(root, i) &gt; max) {
                  max = numNodesLevel(root, i);
              }
          }
          return max;
      }
  }
  
  void fill(struct node* root, int* A, int level, int* i) {
      if (root != NULL) {
          if (level == 0) {
              A[*i] = root-&gt;data;
              *i = *i + 1;
          }
          fill(root-&gt;left, A, level - 1, i);
          fill(root-&gt;right, A, level - 1, i);
      }
  }
  
  int* createArray(struct node* root, int level) {
      if (root != NULL) {
          int *A = (int*)calloc(maxNodesLevel(root), sizeof(int));
          int i = 0;
          fill(root, A, level, &amp;i);
          return A;
      }
  }
  
  int main(){
      struct node* root = newNode(12);
      root-&gt;left = newNode(3);
      root-&gt;right = newNode(16);
      root-&gt;left-&gt;left = newNode(2);
      root-&gt;left-&gt;right = newNode(2);
      root-&gt;right-&gt;left = newNode(2);
      root-&gt;right-&gt;right = newNode(22);
      printf("%d", createArray(root,1));
      getchar();
      return 0;
  }
  </code></pre>
  
  <p>The result should be 3,16 which are the values of the nodes at level 1, but it gives me different numbers like 22721648. These numbers are different every time I run the code, so I think there must be something wrong in the way I use pointers, but I can't figure out where is the error.
  Can somebody help me?</p>