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

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
static const int NIL = -1;
static const int NumberChild = 2;
class Tree {
private:
  int id;
  int parent;
  int child[NumberChild];

public:
  Tree() { id = child[0] = child[1] = parent = NIL; }
  void AddParent(int ID) {
    parent = ID;
    return;
  }
  void AddID(int ID) {
    id = ID;
    return;
  }
  void AddChild(std::vector<int> &Child) {
    for (unsigned int i = 0; i < Child.size(); ++i) {
      child[i] = Child[i];
    }

    return;
  }
  int GetRoot(std::vector<Tree> nodes, int ID) {
    int RootID = ID;
    if (RootID != NIL && nodes[RootID].parent != NIL) {
      RootID = GetRoot(nodes, nodes[RootID].parent);
    }
    return RootID;
  }
  void PreorderTreeWalk(std::vector<Tree> nodes, int ID) {
    if (ID != NIL) {
      std::cout << " " << nodes[ID].id;
      PreorderTreeWalk(nodes, nodes[ID].child[0]);
      PreorderTreeWalk(nodes, nodes[ID].child[1]);
    }

    return;
  }
  void InorderTreeWalk(std::vector<Tree> nodes, int ID) {
    if (ID != NIL) {
      InorderTreeWalk(nodes, nodes[ID].child[0]);
      std::cout << " " << nodes[ID].id;
      InorderTreeWalk(nodes, nodes[ID].child[1]);
    }

    return;
  }
  void PostorderTreeWalk(std::vector<Tree> nodes, int ID) {
    if (ID != NIL) {

      PostorderTreeWalk(nodes, nodes[ID].child[0]);
      PostorderTreeWalk(nodes, nodes[ID].child[1]);
      if (nodes[ID].id != NIL)
        std::cout << " " << nodes[ID].id;
    }

    return;
  }
  void GetVctorPostorderTreeWalk(std::vector<Tree> nodes,
                                 std::vector<int> &returnvector, int ID) {

    if (ID != NIL) {

      GetVctorPostorderTreeWalk(nodes, returnvector, nodes[ID].child[0]);
      GetVctorPostorderTreeWalk(nodes, returnvector, nodes[ID].child[1]);
      if (nodes[ID].id != NIL)
        returnvector.push_back(nodes[ID].id);
    }

    return;
  }
  void Reconstruct(std::vector<int> Pre, std::vector<int> In,
                   std::vector<Tree> &nodes, const int Root, int &cnt) {

    unsigned int m =
        std::distance(In.begin(), std::find(In.begin(), In.end(), Root));

    // nodes[cnt].AddID(Root);
    if (Root == NIL) {
      return;
    }
    nodes[Root].AddID(Root);

    if (m == 0) {
      std::vector<int> RestPre;
      RestPre.push_back(NIL);
      if (m < Pre.size() - 1) {
        RestPre.pop_back();
        std::copy(&Pre[m] + 1, &Pre[Pre.size() - 1] + 1,
                  std::back_inserter(RestPre));
      }
      std::vector<int> RestIn;
      RestIn.push_back(NIL);
      if (m < In.size() - 1) {
        RestIn.pop_back();
        std::copy(&In[m] + 1, &In[In.size() - 1] + 1,
                  std::back_inserter(RestIn));
      }
      std::vector<int> child(2);
      child[0] = NIL;
      child[1] = RestPre.empty() ? NIL : RestPre[0];
      nodes[Root].AddChild(child);
      for (unsigned int i = 0; i < child.size(); ++i) {

        if (child[i] != NIL) {
          nodes[child[i]].AddParent(Root);
        }
      }
      Reconstruct(RestPre, RestIn, nodes, RestPre[0], ++cnt);
      return;
    }
    std::vector<int> PreLeft;
    std::copy(&Pre[1], &Pre[m] + 1, std::back_inserter(PreLeft));
    std::vector<int> InLeft;
    std::vector<int> PreRight;
    PreRight.push_back(NIL);
    // PreRight={NIL};
    std::vector<int> InRight;
    InRight.push_back(NIL);
    // InRight={NIL};
    std::copy(&In[0], &In[m - 1] + 1, std::back_inserter(InLeft));

    if (m < Pre.size() - 1) {
      PreRight.pop_back();
      std::copy(&Pre[m] + 1, &Pre[Pre.size() - 1] + 1,
                std::back_inserter(PreRight));
    }
    if (m < In.size() - 1) {
      InRight.pop_back();
      std::copy(&In[m] + 1, &In[In.size() - 1] + 1,
                std::back_inserter(InRight));
    }

    std::vector<int> child(2);
    child[0] = PreLeft.empty() ? NIL : PreLeft[0];
    child[1] = PreRight.empty() ? NIL : PreRight[0];
    // nodes[cnt].AddChild(child);
    nodes[Root].AddChild(child);
    for (unsigned int i = 0; i < child.size(); ++i) {

      if (child[i] != NIL) {
        nodes[child[i]].AddParent(Root);
      }
    }
    Reconstruct(PreLeft, InLeft, nodes, PreLeft[0], ++cnt);
    Reconstruct(PreRight, InRight, nodes, PreRight[0], ++cnt);
  }
};

void SetVector(std::vector<int> &InputVector) {
  for (unsigned int i = 0; i < InputVector.size(); ++i) {
    std::cin >> InputVector[i];
  }
  return;
}
int main(void) {

  int n;
  std::cin >> n;
  std::vector<int> Pre(n);
  std::vector<int> In(n);
  SetVector(Pre);
  SetVector(In);
  std::vector<Tree> nodes(n + 1);
  // ReconstructWithNoParent
  int cnt = 0;
  nodes[1].Reconstruct(Pre, In, nodes, Pre[0], cnt);

  // nodes[1].PostorderTreeWalk(nodes, 1);
  std::vector<int> Print;
  nodes[1].GetVctorPostorderTreeWalk(nodes, Print, nodes[1].GetRoot(nodes, 1));
  // Print=nodes[1].GetVctorPostorderTreeWalk(nodes, 1);
  for (unsigned int i = 0; i < Print.size(); ++i) {
    if (i) {
      std::cout << " ";
    }
    std::cout << Print[i];
    if (i == Print.size() - 1) {
      std::cout << std::endl;
    }
  }

  //	system("pause");
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
static const int NIL = -1;
static const int NumberChild = 2;
class Tree {
private:
  int id;
  int parent;
  int child[NumberChild];

public:
  Tree() { id = child[0] = child[1] = parent = NIL; }
  void AddParent(int ID) {
    parent = ID;
    return;
  }
  void AddID(int ID) {
    id = ID;
    return;
  }
  void AddChild(std::vector<int> &Child) {
    for (unsigned int i = 0; i < Child.size(); ++i) {
      child[i] = Child[i];
    }

    return;
  }
  int GetRoot(std::vector<Tree> nodes, int ID) {
    int RootID = ID;
    if (RootID != NIL && nodes[RootID].parent != NIL) {
      RootID = GetRoot(nodes, nodes[RootID].parent);
    }
    return RootID;
  }
  void PreorderTreeWalk(std::vector<Tree> nodes, int ID) {
    if (ID != NIL) {
      std::cout << " " << nodes[ID].id;
      PreorderTreeWalk(nodes, nodes[ID].child[0]);
      PreorderTreeWalk(nodes, nodes[ID].child[1]);
    }

    return;
  }
  void InorderTreeWalk(std::vector<Tree> nodes, int ID) {
    if (ID != NIL) {
      InorderTreeWalk(nodes, nodes[ID].child[0]);
      std::cout << " " << nodes[ID].id;
      InorderTreeWalk(nodes, nodes[ID].child[1]);
    }

    return;
  }
  void PostorderTreeWalk(std::vector<Tree> nodes, int ID) {
    if (ID != NIL) {

      PostorderTreeWalk(nodes, nodes[ID].child[0]);
      PostorderTreeWalk(nodes, nodes[ID].child[1]);
      if (nodes[ID].id != NIL)
        std::cout << " " << nodes[ID].id;
    }

    return;
  }
  void GetVctorPostorderTreeWalk(std::vector<Tree> nodes,
                                 std::vector<int> &returnvector, int ID) {

    if (ID != NIL) {

      GetVctorPostorderTreeWalk(nodes, returnvector, nodes[ID].child[0]);
      GetVctorPostorderTreeWalk(nodes, returnvector, nodes[ID].child[1]);
      if (nodes[ID].id != NIL)
        returnvector.push_back(nodes[ID].id);
    }

    return;
  }
  void Reconstruct(const std::vector<int> Pre, const std::vector<int> In,
                   std::vector<Tree> &nodes, const int Root, int &cnt) {

    int m = std::distance(In.begin(), std::find(In.begin(), In.end(), Root));

    // nodes[cnt].AddID(Root);
    nodes[Root].AddID(Root);
    if (m == 0 || Root == NIL) {
      return;
    }
    std::vector<int> PreLeft;

    std::copy(&Pre[1], &Pre[m] + 1, std::back_inserter(PreLeft));

    std::vector<int> PreRight;

    std::copy(&Pre[m + 1], &Pre[Pre.size() - 1] + 1,
              std::back_inserter(PreRight));

    std::vector<int> InLeft;
    std::copy(&In[0], &In[m - 1] + 1, std::back_inserter(InLeft));

    std::vector<int> InRight;
    std::copy(&In[m] + 1, &In[In.size() - 1] + 1, std::back_inserter(InRight));

    std::vector<int> child(2);
    child[0] = PreLeft.empty() ? NIL : PreLeft[0];
    child[1] = PreRight.empty() ? NIL : PreRight[0];
    // nodes[cnt].AddChild(child);
    nodes[Root].AddChild(child);
    for (unsigned int i = 0; i < child.size(); ++i) {

      nodes[child[i]].AddParent(Root);
    }
    Reconstruct(PreLeft, InLeft, nodes, PreLeft[0], ++cnt);
    Reconstruct(PreRight, InRight, nodes, PreRight[0], ++cnt);
  }
};

void SetVector(std::vector<int> &InputVector) {
  for (unsigned int i = 0; i < InputVector.size(); ++i) {
    std::cin >> InputVector[i];
  }
  return;
}
int main(void) {

  int n;
  std::cin >> n;
  std::vector<int> Pre(n);
  std::vector<int> In(n);
  SetVector(Pre);
  SetVector(In);
  std::vector<Tree> nodes(n + 1);
  // ReconstructWithNoParent
  int cnt = 0;
  nodes[1].Reconstruct(Pre, In, nodes, Pre[0], cnt);

  // nodes[1].PostorderTreeWalk(nodes, 1);
  std::vector<int> Print;
  nodes[1].GetVctorPostorderTreeWalk(nodes, Print, 1);
  // Print=nodes[1].GetVctorPostorderTreeWalk(nodes, 1);
  for (unsigned int i = 0; i < Print.size(); ++i) {
    if (i) {
      std::cout << " ";
    }
    std::cout << Print[i];
    if (i == Print.size() - 1) {
      std::cout << std::endl;
    }
  }

  //	system("pause");
  return 0;
}