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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <cmath>
#include <float.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#pragma warning(disable : 4996)

using namespace std;
using Iterator = std::string::iterator;

struct Tree {
  int value_;
  Tree *left_ = nullptr, *right = nullptr;
  Tree(int Value, Tree *l, Tree *r) {
    value_ = Value;
    left_ = l;
    right = r;
  }
};

Tree *CreateTree(Iterator &Begin, Iterator &End) {
  if (Begin == End) {
    return nullptr;
  }
  int idx = 0;
  Iterator lb = Begin + 1, le, rb, re = End - 1;
  for (; Begin != End; ++Begin) {
    if (*Begin == '(')
      ++idx;
    else if (*Begin == ')')
      --idx;
    if (idx == 0) {
      le = Begin;
      string num;
      for (++ ++Begin; Begin != End && *Begin != ']'; ++Begin) {
        num += *Begin;
      }
      rb = Begin + 2;
      return new Tree(stoi(num), CreateTree(lb, le), CreateTree(rb, re));
    }
  }
}

Tree *AddTree(Tree *a, Tree *b) {
  Tree *ret = new Tree(a->value_ + b->value_, nullptr, nullptr);
  if (a->left_ != nullptr && b->left_ != nullptr) {
    ret->left_ = AddTree(a->left_, b->left_);
  }
  if (a->right != nullptr && b->right != nullptr) {
    ret->right = AddTree(a->right, b->right);
  }
  return ret;
}

string ToString(Tree *t) {
  return "(" + (t->left_ == nullptr ? "" : ToString(t->left_)) + ")[" +
         to_string(t->value_) + "](" +
         (t->right == nullptr ? "" : ToString(t->right)) + ")";
}

int main() {
  string a, b;
  cin >> a >> b;
  auto ab = a.begin(), ae = a.end(), bb = b.begin(), be = b.end();
  auto *at = CreateTree(ab, ae), *bt = CreateTree(bb, be);
  auto *ans = AddTree(at, bt);
  cout << ToString(ans) << endl;
  return 0;
}

Error: -11

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <cmath>
#include <float.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#pragma warning(disable : 4996)

using namespace std;
using Iterator = std::string::iterator;

struct Tree {
  int value_;
  Tree *left_ = nullptr, *right = nullptr;
  Tree(int Value, Tree *l, Tree *r) {
    value_ = Value;
    left_ = l;
    right = r;
  }
};

Tree *CreateTree(Iterator &Begin, Iterator &End) {
  if (Begin == End) {
    return nullptr;
  }
  int idx = 0;
  Iterator lb = Begin + 1, le, rb, re = End;
  for (; Begin != End; ++Begin) {
    if (*Begin == '(')
      ++idx;
    else if (*Begin == ')')
      --idx;
    if (idx == 0) {
      le = Begin;
      string num;
      for (++ ++Begin; Begin != End && *Begin != ']'; ++Begin) {
        num += *Begin;
      }
      rb = Begin + 2;
      return new Tree(stoi(num), CreateTree(lb, le), CreateTree(rb, re));
    }
  }
}

Tree *AddTree(Tree *a, Tree *b) {
  Tree *ret = new Tree(a->value_ + b->value_, nullptr, nullptr);
  if (a->left_ != nullptr && b->left_ != nullptr) {
    ret->left_ = AddTree(a->left_, b->left_);
  }
  if (a->right != nullptr && b->right != nullptr) {
    ret->right = AddTree(a->right, b->right);
  }
  return ret;
}

string ToString(Tree *t) {
  return "(" + (t->left_ == nullptr ? "" : ToString(t->left_)) + ")[" +
         to_string(t->value_) + "](" +
         (t->right == nullptr ? "" : ToString(t->right)) + ")";
}

int main() {
  string a, b;
  cin >> a >> b;
  auto ab = a.begin(), ae = a.end() - 1, bb = b.begin(), be = b.end() - 1;
  auto *at = CreateTree(ab, ae), *bt = CreateTree(bb, be);
  auto *ans = AddTree(at, bt);
  cout << ToString(ans) << endl;
  return 0;
}