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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

struct TNode {
  int Index;
  TNode *Parent;
  TNode *Left;
  TNode *Right;

  TNode *PreOrderNext;
  std::vector<int>::iterator InOrder;

  TNode();
};

TNode::TNode() {
  Index = -1;
  Parent = NULL;
  Left = NULL;
  Right = NULL;
  PreOrderNext = NULL;
}

typedef std::vector<TNode> TBinaryTree;

TNode *InputNodes(TBinaryTree &Tree, std::vector<int> &InOrder) {
  TNode *Temp = NULL;
  TNode *Root = NULL;
  for (unsigned int i = 0; i < Tree.size(); ++i) {
    int Index;
    std::cin >> Index;
    Tree[Index - 1].Index = Index;
    if (Temp)
      Temp->PreOrderNext = &(Tree[Index - 1]);
    Temp = &(Tree[Index - 1]);
    if (i == 0)
      Root = Temp;
  }

  std::vector<int>::iterator It = InOrder.begin();
  for (unsigned int i = 0; i < Tree.size(); ++i) {
    int Index;
    std::cin >> Index;
    Tree[Index - 1].InOrder = It;
    InOrder[i] = Index;
    ++It;
  }
  return Root;
}

TNode *GetPreOrderNext(TNode *Node, int Count) {
  TNode *Next = Node;
  for (int i = 0; i < Count && Next != NULL; ++i)
    Next = Next->PreOrderNext;
  return Next;
}

void SetChildren(TNode *Root, std::vector<int>::iterator InOrderBegin,
                 std::vector<int>::iterator InOrderEnd) {
  if (Root->PreOrderNext == NULL)
    return;
  int Position = distance(InOrderBegin, Root->InOrder);
  if (Root->PreOrderNext->Parent == NULL && Position > 0) {
    Root->Left = Root->PreOrderNext;
    Root->PreOrderNext->Parent = Root;
    SetChildren(Root->Left, InOrderBegin, Root->InOrder);
  }
  if (Root->InOrder != InOrderEnd - 1) {
    Root->Right =
        GetPreOrderNext(Root, 1 + distance(InOrderBegin, Root->InOrder));
    Root->Right->Parent = Root;
    SetChildren(Root->Right, Root->InOrder + 1, InOrderEnd);
  }
}

void OutputPostOrder(const TNode *Root, const TNode *End) {
  if (Root == NULL)
    return;
  OutputPostOrder(Root->Left, End);
  OutputPostOrder(Root->Right, End);
  std::cout << Root->Index;
  if (Root != End) {
    std::cout << " ";
  } else {
    std::cout << std::endl;
  }
}

int main() {
  int Count;
  std::cin >> Count;
  TBinaryTree BinaryTree(Count);
  std::vector<int> InOrder(Count);

  TNode *Root = InputNodes(BinaryTree, InOrder);

  SetChildren(Root, InOrder.begin(), InOrder.end());

  OutputPostOrder(Root, Root);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

struct TNode {
  int Index;
  TNode *Parent;
  TNode *Left;
  TNode *Right;

  TNode *PreOrderNext;
  std::vector<int>::iterator InOrder;

  TNode();
};

TNode::TNode() {
  Index = -1;
  Parent = NULL;
  Left = NULL;
  Right = NULL;
  PreOrderNext = NULL;
}

typedef std::vector<TNode> TBinaryTree;
const TNode *GetRoot(const TBinaryTree &Tree) {
  const TNode *Node = &(Tree[0]);
  while (Node->Parent) {
    Node = Node->Parent;
  }
  return Node;
}

void InputNodes(TBinaryTree &Tree, std::vector<int> &InOrder) {
  TNode *Temp = NULL;
  for (unsigned int i = 0; i < Tree.size(); ++i) {
    int Index;
    std::cin >> Index;
    Tree[Index - 1].Index = Index;
    if (Temp)
      Temp->PreOrderNext = &(Tree[Index - 1]);
    Temp = &(Tree[Index - 1]);
  }

  std::vector<int>::iterator It = InOrder.begin();
  for (unsigned int i = 0; i < Tree.size(); ++i) {
    int Index;
    std::cin >> Index;
    Tree[Index - 1].InOrder = It;
    InOrder[i] = Index;
    ++It;
  }
}

TNode *GetPreOrderNext(TNode *Node, int Count) {
  TNode *Next = Node;
  for (int i = 0; i < Count && Next != NULL; ++i)
    Next = Next->PreOrderNext;
  return Next;
}

void SetChildren(TNode *Root, std::vector<int>::iterator InOrderBegin,
                 std::vector<int>::iterator InOrderEnd) {
  if (Root->PreOrderNext == NULL)
    return;
  int Position = distance(InOrderBegin, Root->InOrder);
  if (Root->PreOrderNext->Parent == NULL && Position > 0) {
    Root->Left = Root->PreOrderNext;
    Root->PreOrderNext->Parent = Root;
    SetChildren(Root->Left, InOrderBegin, Root->InOrder);
  }
  if (Root->InOrder != InOrderEnd - 1) {
    Root->Right =
        GetPreOrderNext(Root, 1 + distance(InOrderBegin, Root->InOrder));
    Root->Right->Parent = Root;
    SetChildren(Root->Right, Root->InOrder + 1, InOrderEnd);
  }
}

void OutputPostOrder(const TNode *Root, const TNode *End) {
  if (Root == NULL)
    return;
  OutputPostOrder(Root->Left, End);
  OutputPostOrder(Root->Right, End);
  std::cout << Root->Index;
  if (Root != End) {
    std::cout << " ";
  } else {
    std::cout << std::endl;
  }
}

void OutputWalkOrder(const TBinaryTree &Tree) {
  OutputPostOrder(GetRoot(Tree), GetRoot(Tree));
}

int main() {
  int Count;
  std::cin >> Count;
  TBinaryTree BinaryTree(Count);
  std::vector<int> InOrder(Count);

  InputNodes(BinaryTree, InOrder);

  SetChildren(&(BinaryTree[0]), InOrder.begin(), InOrder.end());

  OutputWalkOrder(BinaryTree);

  return 0;
}