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

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

class BinaryTree {
public:
  BinaryTree *parent;
  BinaryTree *left;
  BinaryTree *right;
  int no;

  BinaryTree() {
    parent = NULL;
    left = NULL;
    right = NULL;
    no = 1;
  }

  BinaryTree(BinaryTree *p) {
    parent = p;
    left = NULL;
    right = NULL;
    no = p->no + 1;
  }
};

vector<BinaryTree> Nodes(5000);
int Nodes_cnt = 0;

BinaryTree getTree(string in) {
  stack<int> lrs;
  int lr = 0;

  BinaryTree root;
  BinaryTree *now;

  bool first = true;
  now = &root;

  for (string::iterator it = in.begin(); it != in.end(); it++) {
    if (first) {
      first = false;
      continue;
    }

    if (*it == '(') {
      if (lr == 0) {
        now->left = new BinaryTree(now);
        now = now->left;
      } else {
        now->right = new BinaryTree(now);
        now = now->right;
      }

      lrs.push(lr);
      lr = 0;
    } else if (*it == ')') {
      if (now->parent != NULL) {
        now = now->parent;
        lr = lrs.top();
        lrs.pop();
      }

    } else if (*it == ',') {
      lr++;
    }
  }

  return root;
}

void printTree(BinaryTree *tree) {
  cout << "(";
  if (tree->left != NULL)
    printTree(tree->left);
  cout << ",";
  if (tree->right != NULL)
    printTree(tree->right);
  cout << ")";
}

BinaryTree *getIntersection(BinaryTree *a, BinaryTree *b, BinaryTree *p) {
  // ツ仰、ツ津環づ按閉板閉ェツづーツ個ゥツづつつッツづゥ
  int nc = Nodes_cnt;
  Nodes_cnt++;
  BinaryTree &node = Nodes[nc];
  if (p == NULL)
    node = BinaryTree();
  else
    node = BinaryTree(p);

  if ((a != NULL && a->left != NULL) && (b != NULL && b->left != NULL)) {
    BinaryTree *next = getIntersection(a == NULL ? NULL : a->left,
                                       b == NULL ? NULL : b->left, &node);
    node.left = next;
  }
  if ((a != NULL && a->right != NULL) && (b != NULL && b->right != NULL)) {
    BinaryTree *next = getIntersection((a == NULL ? NULL : a->right),
                                       (b == NULL ? NULL : b->right), &node);
    node.right = next;
  }

  return &Nodes[nc];
}

BinaryTree *getUnion(BinaryTree *a, BinaryTree *b, BinaryTree *p) {
  // ツづづつソツつゥツづ可甘慊づ慊づェツづつ「ツづゥツづ按づァOK
  int nc = Nodes_cnt;
  Nodes_cnt++;
  BinaryTree &node = Nodes[nc];
  if (p == NULL)
    node = BinaryTree();
  else
    node = BinaryTree(p);

  if ((a != NULL && a->left != NULL) || (b != NULL && b->left != NULL)) {
    BinaryTree *next = getUnion((a == NULL ? NULL : a->left),
                                (b == NULL ? NULL : b->left), &node);
    node.left = next;
  }
  if ((a != NULL && a->right != NULL) || (b != NULL && b->right != NULL)) {
    BinaryTree *next = getUnion((a == NULL ? NULL : a->right),
                                (b == NULL ? NULL : b->right), &node);
    node.right = next;
  }

  return &Nodes[nc];
}

int main(void) {
  string type;
  string in_a;
  string in_b;

  while (cin >> type >> in_a >> in_b) {
    BinaryTree a_root = getTree(in_a);
    BinaryTree b_root = getTree(in_b);

    if (type == "i") {
      BinaryTree *intersectionTree = getIntersection(&a_root, &b_root, NULL);
      printTree(intersectionTree);
      cout << endl;
    } else if (type == "u") {
      BinaryTree *unionTree = getUnion(&a_root, &b_root, NULL);
      printTree(unionTree);
      cout << endl;
    }
  }
}

Error: 0

Fixed submission:

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

#define SIZE 10000

vector<int> getTree(string in) {
  vector<int> tree(SIZE, 0);

  int n = 1;
  int lr = 0;
  stack<int> lrs;

  lrs.push(0);

  for (string::iterator it = in.begin(); it != in.end(); it++) {
    switch (*it) {
    case '(':
      n *= 2;
      tree[n + lr] = 1;
      lrs.push(lr);
      lr = 0;
      break;

    case ')':
      n /= 2;
      lr = lrs.top();
      lrs.pop();
      break;

    case ',':
      lr++;
      break;
    }
  }

  return tree;
}

void printTree(vector<int> &tree, int node) {
  cout << "(";
  if (tree[node * 2] == 1) {
    printTree(tree, node * 2);
  }
  cout << ",";
  if (tree[node * 2 + 1] == 1) {
    printTree(tree, node * 2 + 1);
  }
  cout << ")";
}

vector<int> intersectionTree(vector<int> &a, vector<int> &b) {
  vector<int> tree(SIZE, 0);
  for (int i = 0; i < SIZE; i++)
    if (a[i] == 1 && b[i] == 1)
      tree[i] = 1;
  return tree;
}

vector<int> unionTree(vector<int> &a, vector<int> &b) {
  vector<int> tree(SIZE, 0);
  for (int i = 0; i < SIZE; i++)
    if (a[i] == 1 || b[i] == 1)
      tree[i] = 1;
  return tree;
}

int main(void) {
  string type;
  string in_a;
  string in_b;

  while (cin >> type >> in_a >> in_b) {
    vector<int> a = getTree(in_a);
    vector<int> b = getTree(in_b);

    vector<int> c;
    if (type == "i") {
      c = intersectionTree(a, b);
    } else if (type == "u") {
      c = unionTree(a, b);
    }
    printTree(c, 2);
    cout << endl;
  }
}