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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)

typedef long long ll;
typedef unsigned long long ull;
typedef string str;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vd> vvd;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

struct Node {
  int val;
  Node *l;
  Node *r;
};
const Node Ni = {0, NULL, NULL};

void dfs(Node *a, string s) {
  int cnt = 0;
  *a = Ni;
  REP(i, s.length()) {
    if (s[i] == '(') {
      cnt++;
    }
    if (s[i] == ')') {
      cnt--;
    }
    if (s[i] == '[' && cnt == 0) {
      int llastidx = i - 1;
      i++;
      while (isdigit(s[i])) {
        a->val *= 10;
        a->val += s[i] - '0';
        i++;
      }
      string s2;
      s2 = s.substr(1, llastidx - 1);
      if (s2 != "") {
        a->l = (Node *)malloc(sizeof(Node));
        dfs(a->l, s.substr(1, llastidx - 1));
      }
      s2 = s.substr(i + 2, s.length() - (i + 2) - 1);
      if (s2 != "") {
        a->r = (Node *)malloc(sizeof(Node));
        dfs(a->r, s.substr(i + 2, s.length() - (i + 2) - 1));
      }
      break;
    }
  }
  return;
}
string add(Node *a, Node *b) {
  string ret = "";
  int val = a->val + b->val;
  ret += "(";
  if ((a->l != NULL) && (b->l != NULL))
    ret += add(a->l, b->l);
  ret += ")[" + to_string(val) + "](";
  if ((a->r != NULL) && (b->r != NULL))
    ret += add(a->r, b->r);
  ret += ")";
  return ret;
}
void nd(Node *a) {
  if (a->l != NULL)
    nd(a->l);
  if (a->r != NULL)
    nd(a->r);
  free(a);
}
int main() {
  string s1, s2;
  cin >> s1 >> s2;
  Node *a;
  a = (Node *)malloc(sizeof(Node));
  dfs(a, s1);
  Node *b;
  b = (Node *)malloc(sizeof(Node));
  dfs(b, s2);
  cout << add(a, b) << endl;
  nd(a);
  nd(b);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

struct UnionFind {
  vector<int> v;
  UnionFind(int n) : v(n) {
    for (int i = 0; i < n; i++)
      v[i] = i;
  }
  int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
  void unite(int x, int y) { v[find(x)] = find(y); }
};
struct tree {
  int num;
  int left;
  int right;
};

vector<tree> tree1;
vector<tree> tree2;
vector<tree> tree3;

int saiki(string s, vector<tree> &vt) {
  if (s.empty())
    return -1;
  int cnt = 0;
  REP(i, s.length()) {
    if (s[i] == '(') {
      cnt++;
      continue;
    }
    if (s[i] == ')') {
      cnt--;
      continue;
    }
    if (cnt == 0 && isdigit(s[i])) {
      int idx = i;
      int num = 0;
      while (idx < s.length() && isdigit(s[idx])) {
        num *= 10;
        num += s[idx] - '0';
        idx++;
      }
      int no = 0;
      if (!vt.empty())
        no = vt.size();
      tree t;
      vt.push_back(t);
      vt[no].num = num;
      vt[no].left = saiki(s.substr(1, i - 3), vt);
      vt[no].right = saiki(s.substr(idx + 2, s.length() - idx - 2), vt);
      return no;
    }
  }
  return -1;
}
int saiki2(int a, int b, vector<tree> &vt3) {
  int no = 0;
  if (!vt3.empty())
    no = vt3.size();
  tree t;
  vt3.push_back(t);

  vt3[no].num = tree1[a].num + tree2[b].num;
  if (tree1[a].left != -1 && tree2[b].left != -1)
    vt3[no].left = saiki2(tree1[a].left, tree2[b].left, vt3);
  else
    vt3[no].left = -1;
  if (tree1[a].right != -1 && tree2[b].right != -1)
    vt3[no].right = saiki2(tree1[a].right, tree2[b].right, vt3);
  else
    vt3[no].right = -1;
  return no;
}

string saiki3(vector<tree> vt, int no) {
  if (vt.size() < no)
    return "";
  string s = "[" + to_string(vt[no].num) + "]";
  string ret = "";
  if (vt[no].left == -1)
    ret += "()";
  else
    ret += "(" + saiki3(vt, vt[no].left) + ")";
  ret += s;
  if (vt[no].right == -1)
    ret += "()";
  else
    ret += "(" + saiki3(vt, vt[no].right) + ")";
  return ret;
}

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  saiki(s1, tree1);
  saiki(s2, tree2);
  // saiki2(0, 0,tree3);
  cout << saiki3(tree3, 0) << endl;
  return 0;
}