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

#include <iostream>
#include <string>
using namespace std;

string tree_op(string t1, string t2, string op) {
  if (t1.empty())
    if (op == "i")
      return "";
    else
      return t2;
  if (t2.empty())
    if (op == "i")
      return "";
    else
      return t1;

  int n1 = 0, n2 = 0;
  int p1 = 0, p2 = 0;
  while (1) {
    if (t1[p1] == '(')
      n1++;
    else if (t1[p1] == ')')
      n1--;
    else if (t1[p1] == ',')
      if (n1 == 1)
        break;
    p1++;
  }
  while (1) {
    if (t2[p2] == '(')
      n2++;
    else if (t2[p2] == ')')
      n2--;
    else if (t2[p2] == ',')
      if (n2 == 1)
        break;
    p2++;
  }

  return "(" + tree_op(t1.substr(1, p1 - 1), t2.substr(1, p2 - 1), op) + "," +
         tree_op(t1.substr(p1 + 1, t1.size() - 2 - p1),
                 t2.substr(p2 + 1, t2.size() - 2 - p2), op) +
         ")";
}

string union_tree(string t1, string t2) {
  if (t1.empty())
    return t2;
  if (t2.empty())
    return t1;
  int n1 = 0, n2 = 0;
  int p1 = 0, p2 = 0;
  while (1) {
    if (t1[p1] == '(')
      n1++;
    else if (t1[p1] == ')')
      n1--;
    else if (t1[p1] == ',')
      if (n1 == 1)
        break;
    p1++;
  }
  while (1) {
    if (t2[p2] == '(')
      n2++;
    else if (t2[p2] == ')')
      n2--;
    else if (t2[p2] == ',')
      if (n2 == 1)
        break;
    p2++;
  }
  return "(" + union_tree(t1.substr(1, p1 - 1), t2.substr(1, p2 - 1)) + "," +
         union_tree(t1.substr(p1 + 1, t1.size() - 2 - p1),
                    t2.substr(p2 + 1, t2.size() - 2 - p2)) +
         ")";
}

int main() {
  string str;
  string op;

  while (getline(cin, str)) {
    op = str[0];

    int k = 1;
    while (str[k] == ' ')
      k++;

    string tree1, tree2;
    while (str[k] != ' ') {
      tree1.push_back(str[k]);
      k++;
    }
    while (str[k] == ' ')
      k++;
    while (k < str.size()) {
      tree2.push_back(str[k]);
      k++;
    }
    cout << tree_op(tree1, tree2, op) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

bool tree[2][100];
enum state { l, r };

string draw_tree(int c, int n) {
  if (c > n)
    return "";
  if (!tree[0][c])
    return "";
  int l = 2 * c + 1;
  int r = 2 * c + 2;
  return "(" + draw_tree(l, n) + "," + draw_tree(r, n) + ")";
}

int main() {
  string str;
  char op;
  int maxn[2] = {0, 0};

  while (getline(cin, str)) {
    for (int i = 0; i < 100; i++) {
      tree[0][i] = false;
      tree[1][i] = false;
    }
    op = str[0];

    int k = 1;
    while (str[k] == ' ')
      k++;

    int p = 0;
    int n = -1;
    state s;
    while (k < str.size()) {
      if (str[k] == ' ') {
        p = 1;
        n = -1;
      } else {
        if (str[k] == '(') {
          if (n == -1) {
            n = 0;
          } else if (s == l)
            n = 2 * n + 1;
          else
            n = 2 * n + 2;

          tree[p][n] = true;
          maxn[p] = max(maxn[p], n);
          s = l;
        } else if (str[k] == ',') {
          s = r;
        } else {
          n = (n - 1) / 2;
        }
      }
      k++;
    }
    // End tree construction

    int m = max(maxn[0], maxn[1]);
    for (int i = 0; i <= m; i++) {
      if (op == 'i')
        tree[0][i] = tree[0][i] && tree[1][i];
      else
        tree[0][i] = tree[0][i] || tree[1][i];
    }

    int num = 0;
    for (int i = 0; i <= m; i++)
      if (tree[0][i])
        num++;
    cout << draw_tree(0, num) << endl;
  }

  return 0;
}