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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define dump(n) cout << "# " << #n << "=" << (n) << endl
#define debug(n)                                                               \\
  cout << __FILE__ << "," << __LINE__ << ": #" << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, n) repi(i, 0, n)
#define iter(c) __typeof((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define allof(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

struct Node {
  Node *left, *right;

  Node() : left(0), right(0) {}
  Node(Node *l, Node *r) : left(l), right(r) {}
  ~Node() {
    delete left;
    delete right;
  }
};

int parse(const string &s, int index, Node *n) {
  index++;
  if (s[index] == '(') {
    n->left = new Node;
    index = parse(s, index, n->left);
  }
  index++;
  if (s[index] == '(') {
    n->right = new Node;
    index = parse(s, index, n->right);
  }
  index++;
  return index;
}

void print(Node *n) {
  if (!n)
    return;
  // if(!n->left && !n->right){
  //	printf("o");
  //	return;
  // }
  printf("(");
  print(n->left);
  printf(",");
  print(n->right);
  printf(")");
}

Node *tree_and(Node *n1, Node *n2) {
  if (!n1 || !n2)
    return NULL;
  Node *n = new Node;
  if (n1->left && n2->left)
    n->left = tree_and(n1->left, n2->left);
  if (n1->right && n2->right)
    n->right = tree_and(n1->right, n2->right);
  return n;
}

Node *tree_or(Node *n1, Node *n2) {
  if (!n1 && !n2)
    return NULL;
  Node *n = new Node;
  if (n1 && n2) {
    if (n1->left || n2->left)
      n->left = tree_or(n1->left, n2->left);
    if (n1->right || n2->right)
      n->right = tree_or(n1->right, n2->right);
  } else if (n1) {
    if (n1->left)
      n->left = tree_or(n1->left, NULL);
    if (n1->right)
      n->right = tree_or(n1->right, NULL);
  } else {
    if (n2->left)
      n->left = tree_or(NULL, n2->left);
    if (n2->right)
      n->right = tree_or(NULL, n2->right);
  }
  return n;
}

int main() {
  for (string op, s1, s2; cin >> op >> s1 >> s2;) {
    // dump(s1);
    // dump(s2);
    Node n1, n2;
    parse(s1, 0, &n1);
    parse(s2, 0, &n2);
    // print(&n1);puts("");
    // print(&n2);puts("");

    Node *res;
    if (op == "i")
      res = tree_and(&n1, &n2);
    else
      res = tree_or(&n1, &n2);
    print(res);
    puts("");

    delete res;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define dump(n) cout << "# " << #n << "=" << (n) << endl
#define debug(n)                                                               \\
  cout << __FILE__ << "," << __LINE__ << ": #" << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, n) repi(i, 0, n)
#define iter(c) __typeof((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define allof(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

struct Node {
  Node *left, *right;

  Node() : left(0), right(0) {}
  Node(Node *l, Node *r) : left(l), right(r) {}
  //~Node(){delete left;delete right;}
};

int parse(const string &s, int index, Node *n) {
  index++;
  if (s[index] == '(') {
    n->left = new Node;
    index = parse(s, index, n->left);
  }
  index++;
  if (s[index] == '(') {
    n->right = new Node;
    index = parse(s, index, n->left);
  }
  index++;
  return index;
}

void print(Node *n) {
  if (!n)
    return;
  printf("(");
  print(n->left);
  printf(",");
  print(n->right);
  printf(")");
}

Node *tree_and(Node *n1, Node *n2) {
  if (!n1 || !n2)
    return NULL;
  Node *n = new Node;
  if (n1->left && n2->left)
    n->left = tree_and(n1->left, n2->left);
  if (n1->right && n2->right)
    n->right = tree_and(n1->right, n2->right);
  return n;
}

Node *tree_or(Node *n1, Node *n2) {
  if (!n1 && !n2)
    return NULL;
  Node *n = new Node;
  if (n1 && n2) {
    if (n1->left || n2->left)
      n->left = tree_or(n1->left, n2->left);
    if (n1->right || n2->right)
      n->right = tree_or(n1->right, n2->right);
  } else if (n1) {
    if (n1->left)
      n->left = tree_or(n1->left, NULL);
    if (n1->right)
      n->right = tree_or(n1->right, NULL);
  } else {
    if (n2->left)
      n->left = tree_or(NULL, n2->left);
    if (n2->right)
      n->right = tree_or(NULL, n2->right);
  }
  return n;
}

int main() {
  for (string op, s1, s2; cin >> op >> s1 >> s2;) {
    Node n1, n2;
    parse(s1, 0, &n1);
    parse(s2, 0, &n2);

    Node *res;
    if (op == "i")
      res = tree_and(&n1, &n2);
    else
      res = tree_or(&n1, &n2);
    print(res);
    puts("");
  }

  return 0;
}