Problem p02280 (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();
};

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

typedef std::vector<TNode> TBinaryTree;

void InputNode(TBinaryTree &Tree) {
  int Index;
  std::cin >> Index;
  Tree[Index].Index = Index;

  int Left;
  std::cin >> Left;
  if (Left >= 0) {
    Tree[Index].Left = &(Tree[Left]);
    Tree[Left].Parent = &(Tree[Index]);
  }

  int Right;
  std::cin >> Right;
  if (Right >= 0) {
    Tree[Index].Right = &(Tree[Right]);
    Tree[Right].Parent = &(Tree[Index]);
  }
}

int GetParentIndex(const TNode &Node) {
  return Node.Parent != NULL ? Node.Parent->Index : -1;
}

int GetSibling(const TNode &Node) {
  if (Node.Parent == NULL)
    return -1;
  if (Node.Parent->Left == NULL || Node.Parent->Right == NULL)
    return -1;
  int Sibling = Node.Parent->Left->Index;
  if (Sibling != Node.Index)
    return Sibling;
  return Node.Parent->Right->Index;
}

int GetDegree(const TNode &Node) {
  int Degree = 0;
  if (Node.Left)
    Degree++;
  if (Node.Right)
    Degree++;
  return Degree;
}

int GetDepth(const TNode &Node) {
  int Depth = 0;
  TNode *Counter = Node.Parent;
  while (Counter) {
    Counter = Counter->Parent;
    Depth++;
  }
  return Depth;
}

int GetHeight(const TNode *Node, int Height = 0) {
  int LeftHeight = Height;
  if (Node->Left)
    LeftHeight = GetHeight(Node->Left, Height + 1);
  int RightHeight = Height;
  if (Node->Right)
    RightHeight = GetHeight(Node->Right, Height + 1);
  return std::max(LeftHeight, RightHeight);
}

std::string GetType(const TNode &Node) {
  if (Node.Parent == NULL)
    return "root";
  if (!(Node.Left || Node.Right))
    return "leaf";
  return "internal node";
}

void OutputNode(const TNode &Node) {
  std::cout << "node " << Node.Index << ": ";
  std::cout << "parent = " << GetParentIndex(Node) << ", ";
  std::cout << "sibling = " << GetSibling(Node) << ", ";
  std::cout << "degree = " << GetDegree(Node) << ", ";
  std::cout << "depth = " << GetDepth(Node) << ", ";
  std::cout << "height = " << GetHeight(&Node) << ", ";
  std::cout << GetType(Node) << std::endl;
}

int main() {
  int Count;
  std::cin >> Count;
  TBinaryTree BinaryTree(Count);
  for (int i = 0; i < Count; i++) {
    InputNode(BinaryTree);
  }
  for (int i = 0; i < Count; i++) {
    OutputNode(BinaryTree[i]);
  }
  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();
};

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

typedef std::vector<TNode> TBinaryTree;

void InputNode(TBinaryTree &Tree) {
  int Index;
  std::cin >> Index;
  Tree[Index].Index = Index;

  int Left;
  std::cin >> Left;
  if (Left >= 0) {
    Tree[Index].Left = &(Tree[Left]);
    Tree[Left].Parent = &(Tree[Index]);
  }

  int Right;
  std::cin >> Right;
  if (Right >= 0) {
    Tree[Index].Right = &(Tree[Right]);
    Tree[Right].Parent = &(Tree[Index]);
  }
}

int GetParentIndex(const TNode &Node) {
  return Node.Parent != NULL ? Node.Parent->Index : -1;
}

int GetSibling(const TNode &Node) {
  if (!Node.Parent)
    return -1;
  int Sibling = Node.Parent->Left->Index;
  if (Sibling != Node.Index)
    return Sibling;
  return Node.Parent->Right->Index;
}

int GetDegree(const TNode &Node) {
  int Degree = 0;
  if (Node.Left)
    Degree++;
  if (Node.Right)
    Degree++;
  return Degree;
}

int GetDepth(const TNode &Node) {
  int Depth = 0;
  TNode *Counter = Node.Parent;
  while (Counter) {
    Counter = Counter->Parent;
    Depth++;
  }
  return Depth;
}

int GetHeight(const TNode *Node, int Height = 0) {
  int LeftHeight = Height;
  if (Node->Left)
    LeftHeight = GetHeight(Node->Left, Height + 1);
  int RightHeight = Height;
  if (Node->Right)
    RightHeight = GetHeight(Node->Right, Height + 1);
  return std::max(LeftHeight, RightHeight);
}

std::string GetType(const TNode &Node) {
  if (Node.Parent == NULL)
    return "root";
  if (!(Node.Left || Node.Right))
    return "leaf";
  return "internal node";
}

void OutputNode(const TNode &Node) {
  std::cout << "node " << Node.Index << ": ";
  std::cout << "parent = " << GetParentIndex(Node) << ", ";
  std::cout << "sibling = " << GetSibling(Node) << ", ";
  std::cout << "degree = " << GetDegree(Node) << ", ";
  std::cout << "depth = " << GetDepth(Node) << ", ";
  std::cout << "height = " << GetHeight(&Node) << ", ";
  std::cout << GetType(Node) << std::endl;
}

int main() {
  int Count;
  std::cin >> Count;
  TBinaryTree BinaryTree(Count);
  for (int i = 0; i < Count; i++) {
    InputNode(BinaryTree);
  }
  for (int i = 0; i < Count; i++) {
    OutputNode(BinaryTree[i]);
  }
  return 0;
}