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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < eps)
struct node {
  node *l, *r;
  node() : l(NULL), r(NULL) {}
};
string Tree[2];
void string_to_tree(int n, node *cur, int l, int r) {
  int cnt = 0, f;
  for (int i = l + 1; i < r; i++) {
    if (Tree[n][i] == '(')
      cnt++;
    if (Tree[n][i] == ')')
      cnt--;
    if (Tree[n][i] == ',' && cnt == 0) {
      f = i;
      break;
    }
  }
  if (Tree[n][f - 1] == '(' && Tree[n][f + 1] == ')')
    return;
  if (Tree[n][f + 1] == ')') {
    cur->l = new node();
    string_to_tree(n, cur->l, l + 1, f - 1);
  } else if (Tree[n][f - 1] == '(') {
    cur->r = new node();
    string_to_tree(n, cur->r, f + 1, r - 1);
  } else {
    cur->l = new node();
    cur->r = new node();
    string_to_tree(n, cur->l, l + 1, f - 1);
    string_to_tree(n, cur->r, f + 1, r - 1);
  }
  return;
}
node *root[2];
node *ans;
node *tmp;
string Ans;
void tree_to_string(node *cur) {
  if (cur == NULL)
    return;
  Ans += '(';
  tree_to_string(cur->l);
  Ans += ',';
  tree_to_string(cur->r);
  Ans += ')';
  return;
}
void unite(node *cur, node *c1, node *c2) {
  if ((c1->l) != NULL && (c2->l) == NULL) {
    cur->l = new node();
    unite(cur->l, c1->l, tmp);
  } else if ((c1->l) == NULL && (c2->l) != NULL) {
    cur->l = new node();
    unite(cur->l, tmp, c2->l);
  } else if ((c1->l) != NULL && (c2->l) != NULL) {
    cur->l = new node();
    unite(cur->l, c1->l, c2->l);
  }
  if ((c1->r) != NULL && (c2->r) == NULL) {
    cur->r = new node();
    unite(cur->r, c1->r, tmp);
  } else if ((c1->r) == NULL && (c2->r) != NULL) {
    cur->r = new node();
    unite(cur->r, tmp, c2->r);
  } else if ((c1->r) != NULL && (c2->r) != NULL) {
    cur->r = new node();
    unite(cur->r, c1->r, c2->r);
  }
  return;
}
void intersect(node *cur, node *c1, node *c2) {
  if ((c1->l) != NULL && (c2->l) != NULL) {
    cur->l = new node();
    intersect(cur->l, c1->l, c2->l);
  }
  if ((c1->r) != NULL && (c2->r) != NULL) {
    cur->r = new node();
    intersect(cur->r, c1->r, c2->r);
  }
  return;
}
int main() {
  string op;
  tmp = new node();
  while (cin >> op >> Tree[0] >> Tree[1]) {
    Ans.clear();
    root[0] = new node();
    root[1] = new node();
    ans = new node();
    string_to_tree(0, root[0], 0, ((int)Tree[0].size()) - 1);
    string_to_tree(1, root[1], 0, ((int)Tree[1].size()) - 1);
    if (op == "u")
      unite(ans, root[0], root[1]);
    else
      intersect(ans, root[0], root[1]);
    tree_to_string(ans);
    cout << Ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < eps)
bool tree[2][105];
bool ans[105];
string Tree[2];
void string_to_tree(int n, int k, int l, int r) {
  tree[n][k] = true;
  int cnt = 0, node;
  for (int i = l + 1; i < r; i++) {
    if (Tree[n][i] == '(')
      cnt++;
    if (Tree[n][i] == ')')
      cnt--;
    if (Tree[n][i] == ',' && cnt == 0) {
      node = i;
      break;
    }
  }
  if (Tree[n][node - 1] == '(' && Tree[n][node + 1] == ')')
    return;
  if (Tree[n][node + 1] == ')')
    string_to_tree(n, k * 2 + 1, l + 1, node - 1);
  else if (Tree[n][node - 1] == '(')
    string_to_tree(n, k * 2 + 2, node + 1, r - 1);
  else {
    string_to_tree(n, k * 2 + 1, l + 1, node - 1);
    string_to_tree(n, k * 2 + 2, node + 1, r - 1);
  }
  return;
}
string Ans;
void unite() {
  for (int i = 0; i < 105; i++)
    if (tree[0][i] || tree[1][i])
      ans[i] = true;
  return;
}
void intersect() {
  for (int i = 0; i < 105; i++)
    if (tree[0][i] && tree[1][i])
      ans[i] = true;
  return;
}
void tree_to_string(int k) {
  if (!ans[k])
    return;
  Ans += '(';
  tree_to_string(k * 2 + 1);
  Ans += ',';
  tree_to_string(k * 2 + 2);
  Ans += ')';
  return;
}
int main() {
  string op;
  while (cin >> op >> Tree[0] >> Tree[1]) {
    Ans.clear();
    memset(tree, false, sizeof(tree));
    memset(ans, false, sizeof(ans));
    string_to_tree(0, 0, 0, ((int)Tree[0].size()) - 1);
    string_to_tree(1, 0, 0, ((int)Tree[1].size()) - 1);
    if (op == "u")
      unite();
    else
      intersect();
    tree_to_string(0);
    cout << Ans << endl;
  }
  return 0;
}