Language: C++

/*
AizuOnline A1001
Binary Tree Intersection And Union
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int find_comma(char * s)
{ int i;
  int depth=0;
  char c;

  for(i=0;i<strlen(s);i++)
    {
      c = s[i];
      if(c=='(')depth++;
      if(c==')')depth--;
      if((depth==1) && (c == ','))
        return(i);
    }
    return(-1);
}


char *left_node(char *s)
{
    int pos;
    int len;
    char *ptr;
  
     pos=find_comma(s);
     if(pos == 0||pos==1)
          return(NULL);
     else if(pos > 0)
       {  len = pos -1;
          ptr = malloc(len+1);
          strncpy(ptr,&s[1],len);
          ptr[len]='\0';
          return(ptr);
       }

}
char *right_node(char *s)
{
    int pos;
    int len;
    char *ptr;
  
     pos=find_comma(s);
/*printf("P,L = %d %d\n",pos,strlen(s));*/
     if(pos == strlen(s)-1||pos==strlen(s)-2)
          return(NULL);
     else if(pos > 0)
       {  len = strlen(s)-pos -2;
          ptr = malloc(len+1);
          strncpy(ptr,&s[pos+1],len);
          ptr[len]='\0';
          return(ptr);
       }
}

void space_trim(char *s)
{           int i,j;
            for(i=strlen(s);i--;i>=0)
              if(isspace(s[i]))
                for(j=i;j<strlen(s);j++)
                 s[j]=s[j+1];
}

char * binary_tree_cons(char *x,char *y)
{ int len;
  char *p;
  if(NULL == x)
    if(NULL == y)
      {  p=malloc(4);
        strcpy(p,"(,)");
        return(p);
      }
    else
    {  len = strlen(y);
       p=malloc(len+4);
       p[0]='(';
       p[1]=',';
       strcpy(p+2,y);
       p[len+2]=')';
       p[len+3]='\0';
    }
  else
    if(NULL == y)
      { len = strlen(x);
        p=malloc(len+4);
        p[0]='(';
        strcpy(p+1,x);
        p[len+1] = ',';
        p[len+2] = ')';
        p[len+3] = '\0';
      }
    else
      { 
         len = strlen(y)+strlen(x);
         p=malloc(len+4);
         p[0]='(';
         strcpy(p+1,x);
         p[strlen(x)+1]=',';
         strcpy(p+strlen(x)+2,y);
         p[len+2] = ')';
         p[len+3] = '\0';
        
          
        }  
    return(p);
} 


char * binary_tree_union(char *x,char *y)
{ int len;
  char *p;
  if(NULL == x)
    if(NULL == y)
      {  return(NULL);
      }
    else
    {  len = strlen(y);
       p=malloc(len);
       strcpy(p,y);
    }
  else
    if(NULL == y)
      { len = strlen(x);
        p=malloc(len);
        strcpy(p,x);
      }
    else
      { if(0==strcmp(x,y))
        { len = strlen(x);
          p=malloc(len);
          strcpy(p,x);
        }
        else
          {  /* printf("U:%s %s %s %s \n",left_node(x),right_node(x),left_node(y),right_node(y));*/
            p=binary_tree_cons(binary_tree_union (left_node(x),left_node(y)),
                           binary_tree_union (right_node(x),right_node(y)));
          }
      }
  return(p);
} 


char * binary_tree_intersect(char *x,char *y)
{ int len;
  char *p;
  if((NULL == x) || (NULL == y))
      {  return(NULL);
      }
  else
      { if(0==strcmp(x,y))
        { len = strlen(x);
          p=malloc(len);
          strcpy(p,x);
        }
        else
          {  /* printf("I:%s %s %s %s \n",left_node(x),right_node(x),left_node(y),right_node(y)); */
            p=binary_tree_cons(binary_tree_intersect (left_node(x),left_node(y)),
                               binary_tree_intersect (right_node(x),right_node(y)));
          }
      }
  return(p);
} 



main()
{
  char bin1[256],bin2[256];
  char order;

  
  while(3 == scanf(" %c %s %s",&order,bin1,bin2))
    {
/*   printf("T1,T2=%s %s\n",bin1,bin2); */
      

     if(order =='i')
      printf("%s\n",binary_tree_intersect(bin1,bin2));
     else
      printf("%s\n",binary_tree_union(bin2,bin1));


    }
  
  return(0);
}

Compiler output:
a.cc: In function 'char* left_node(char*)':
a.cc:38:23: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
   38 |           ptr = malloc(len+1);
      |                 ~~~~~~^~~~~~~
      |                       |
      |                       void*
a.cc: In function 'char* right_node(char*)':
a.cc:57:23: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
   57 |           ptr = malloc(len+1);
      |                 ~~~~~~^~~~~~~
      |                       |
      |                       void*
a.cc: In function 'char* binary_tree_cons(char*, char*)':
a.cc:77:18: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
   77 |       {  p=malloc(4);
      |            ~~~~~~^~~
      |                  |
      |                  void*
a.cc:83:16: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
   83 |        p=malloc(len+4);
      |          ~~~~~~^~~~~~~
      |                |
      |                void*
a.cc:93:17: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
   93 |         p=malloc(len+4);
      |           ~~~~~~^~~~~~~
      |                 |
      |                 void*
a.cc:103:18: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
  103 |          p=malloc(len+4);
      |            ~~~~~~^~~~~~~
      |                  |
      |                  void*
a.cc: In function 'char* binary_tree_union(char*, char*)':
a.cc:126:16: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
  126 |        p=malloc(len);
      |          ~~~~~~^~~~~
      |                |
      |                void*
a.cc:132:17: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
  132 |         p=malloc(len);
      |           ~~~~~~^~~~~
      |                 |
      |                 void*
a.cc:138:19: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
  138 |           p=malloc(len);
      |             ~~~~~~^~~~~
      |                   |
      |                   void*
a.cc: In function 'char* binary_tree_intersect(char*, char*)':
a.cc:160:19: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
  160 |           p=malloc(len);
      |             ~~~~~~^~~~~
      |                   |
      |                   void*
a.cc: At global scope:
a.cc:174:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  174 | main()
      | ^~~~
a.cc: In function 'char* left_node(char*)':
a.cc:44:1: warning: control reaches end of non-void function [-Wreturn-type]
   44 | }
      | ^
a.cc: In function 'char* right_node(char*)':
a.cc:62:1: warning: control reaches end of non-void function [-Wreturn-type]
   62 | }
      | ^