Problem p00587 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <string>
using namespace std;

struct Node {
  Node *l;
  Node *r;
  Node() { l = r = NULL; }
};

Node *build(char s[], int l, int r) {
  if (r < l)
    return NULL;
  Node *p = new Node();
  int cnt = 0;
  for (int i = l + 1; i < r; ++i) {
    if (s[i] == '(') {
      cnt++;
      continue;
    }
    if (s[i] == ')') {
      cnt--;
      continue;
    }
    if (s[i] == ',' && cnt == 0) {
      p->l = build(s, l + 1, i - 1);
      p->r = build(s, i + 1, r - 1);
      break;
    }
  }
  return p;
}

Node *getInter(Node *a, Node *b) {
  Node *p = new Node();
  if (a->l != NULL && b->l != NULL) {
    p->l = getInter(a->l, b->l);
  }
  // else p->l = NULL;
  if (a->r != NULL && b->r != NULL) {
    p->r = getInter(a->r, b->r);
  }
  // else p->r = NULL;
  return p;
}

Node *getUnion(Node *a, Node *b) {
  Node *p = new Node();
  if (a != NULL && b != NULL) {
    if (a->l != NULL || b->l != NULL) {
      p->l = getUnion(a->l, b->l);
    } else
      p->l = NULL;
    if (a->r != NULL || b->r != NULL) {
      p->r = getUnion(a->r, b->r);
    } else
      p->r = NULL;
  } else if (a == NULL) {
    if (b->l != NULL)
      p->l = getUnion(NULL, b->l);
    else
      p->l = NULL;
    if (b->r != NULL)
      p->r = getUnion(NULL, b->r);
    else
      p->r = NULL;
  } else {
    if (a->l != NULL)
      p->l = getUnion(a->l, NULL);
    else
      p->l = NULL;
    if (a->r != NULL)
      p->r = getUnion(a->r, NULL);
    else
      p->r = NULL;
  }
  return p;
}
void DFS2(Node *root, Node *root1) {
  if (root1->l != NULL) {
    if (root->l == NULL) {
      Node *node = new Node;
      root->l = node;
    }
    DFS2(root->l, root1->l);
  }
  if (root1->r != NULL) {
    if (root->r == NULL) {
      Node *node = new Node;
      root->r = node;
    }
    DFS2(root->r, root1->r);
  }
}
string getStr(Node *p) {
  if (p == NULL) {
    return "";
  } else
    return "(" + getStr(p->l) + "," + getStr(p->r) + ")";
}

char a[3000], b[3000];

int main() {
  char op;
  while (scanf("%c %s %s", &op, a, b) != EOF) {
    getchar();
    int la = strlen(a), lb = strlen(b);
    Node *ra = build(a, 0, la - 1);
    Node *rb = build(b, 0, lb - 1);
    Node *root = new Node;
    if (op == 'i')
      root = getInter(ra, rb);
    else {
      DFS2(root, ra);
      DFS2(root, rb);
    }
    string res = getStr(root);
    printf("%s\\n", res.c_str());
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
stack<int> S;
int node[1010];
string getans(int p) {
  if (node[p] == 0)
    return "";
  else
    return "(" + getans(2 * p) + "," + getans(2 * p + 1) + ")";
}
int main() {
  int treeNode1[1010], treeNode2[1010], i, j;
  char op[2], tree1[10000], tree2[10000];

  while (scanf("%s%s%s", &op, &tree1, &tree2) != EOF) {

    int len1 = strlen(tree1);
    int len2 = strlen(tree2);
    memset(treeNode1, 0, sizeof(treeNode1));
    memset(treeNode2, 0, sizeof(treeNode2));
    int cur;
    for (i = 0; i < len1; i++) {
      if (tree1[i] == '(') {
        if (i == 0) {
          S.push(1);
          treeNode1[1] = 1;
        } else {
          if (tree1[i - 1] == '(') {
            cur = S.top();
            S.push(cur * 2);
            treeNode1[cur * 2] = 1;
          } else {
            cur = S.top();
            S.push(2 * cur + 1);
            treeNode1[cur * 2 + 1] = 1;
          }
        }
      } else if (tree1[i] == ')')
        S.pop();
    }
    for (i = 0; i < len2; i++) {
      if (tree2[i] == '(') {
        if (i == 0) {
          S.push(1);
          treeNode2[1] = 1;
        } else {
          if (tree2[i - 1] == '(') {
            cur = S.top();
            S.push(cur * 2);
            treeNode2[cur * 2] = 1;
          } else {
            cur = S.top();
            S.push(2 * cur + 1);
            treeNode2[cur * 2 + 1] = 1;
          }
        }
      } else if (tree2[i] == ')')
        S.pop();
    }
    // for(i = 0;i < 10;i ++) cout<<treeNode1[i]<<" ";cout<<endl;
    // for(i = 0;i < 10;i ++) cout<<treeNode2[i]<<" ";cout<<endl;
    if (op[0] == 'i') {
      memset(node, 0, sizeof(node));
      for (i = 0; i < 1010; i++) {
        if (treeNode1[i] && treeNode2[i])
          node[i] = 1;
      }
      string ans;
      ans = getans(1);
      cout << ans << endl;
    } else {
      memset(node, 0, sizeof(node));
      for (i = 0; i < 1010; i++) {
        if (treeNode1[i] || treeNode2[i])
          node[i] = 1;
      }
      string ans;
      ans = getans(1);
      cout << ans << endl;
    }
  }
}