title: Create a string with node of BST that has the same value
body: <p>I've a struct like (BST):</p>
  
  <pre><code>typedef struct node {
    char* name;  
    int age;
    struct node* left;       
    struct node* right;      
  } node_t;
  </code></pre>
  
  <p>and i've to make a function like: <code>char* getSameAge(node_t* s, int age)</code> that returns a string like name1:name2:....nameN with node that has == age</p>
  
  <p>I made this solution, it works with tests but using valgrind it gives me errors...does anyone give me an advice or an alternative/better solution?!</p>
  
  <pre><code>void visitTree(node_t* s,char buf[N],int age) {
    if(s!=NULL) {
      visitTree(s-&gt;left,buf,age);
      if(s-&gt;age == age) {
        if(strlen(buf) == 0) strcpy(buf,s-&gt;name);
        else{
         strcat(buf,s-&gt;name);
         if(s-&gt;left != NULL || s-&gt;right != NULL) strcat(buf,":");
        }
      }
      visitTree(s-&gt;right,buf,age);
    }
  }
  
  char* getSameAge(node_t* s, int age) {
  
    char buf[N];
    visitTree(r,buf,age);
    if(strlen(buf) == 0) {
      return NULL;
    }
    else {
      char *aux = malloc(strlen(buf));
      strcpy(aux,buf);
      return aux;
    }
  }
  </code></pre>