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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

// class
// TTable//???????????????????????????TNode*???????????????(vector??????????????¢?????????????????????)
// {
// 	vector<TNode*> Nodes;
// 	~TTable();
// };

struct TNode {
  int Id;
  TNode *Parent;
  TNode *LChild;
  TNode *RChild;
};

enum TNodeType { Root, InternalNode, Leaf };

void Create(int Id, vector<TNode *> &Nodes) {
  if (Nodes[Id] == NULL) {
    TNode *Node;
    Node = new TNode;
    *Node = {Id, NULL, NULL, NULL};
    Nodes[Id] = Node;
  }
}

void CreateLeftRelation(TNode *ParentNode, TNode *LChildNode) {
  ParentNode->LChild = LChildNode;
  LChildNode->Parent = ParentNode;
}

void CreateRightRelation(TNode *ParentNode, TNode *RChildNode) {
  ParentNode->RChild = RChildNode;
  RChildNode->Parent = ParentNode;
}

vector<TNode *> Input() {
  int n;
  cin >> n;
  vector<TNode *> Nodes(n, NULL);
  for (int i = 0; i < n; i++) {
    int Id;
    cin >> Id;
    Create(Id, Nodes);
    int left;
    cin >> left;
    if (left != -1) {
      Create(left, Nodes);
      CreateLeftRelation(Nodes[Id], Nodes[left]);
    }
    int right;
    cin >> right;
    if (right != -1) {
      Create(right, Nodes);
      CreateRightRelation(Nodes[Id], Nodes[right]);
    }
  }
  return Nodes;
}

int GetSibling(const TNode *Node) {
  const TNode *Parent = Node->Parent;
  if (Parent == NULL) {
    return -1;
  }
  if (Node == Parent->LChild && Parent->RChild) {
    return Parent->RChild->Id;
  }
  if (Node == Parent->RChild && Parent->LChild) {
    return Parent->LChild->Id;
  }
  return -1;
}

int GetDegree(const TNode *Node) {
  return (Node->LChild ? 1 : 0) + (Node->RChild ? 1 : 0);
}

int GetDepth(const TNode *Node) {
  int depth = 0;
  while (Node->Parent) {
    depth++;
    Node = Node->Parent;
  }
  return depth;
}

int GetHeight(const TNode *Node) {
  if (Node == NULL)
    return -1;
  return 1 + max(GetHeight(Node->LChild), GetHeight(Node->RChild));
}

TNodeType GetType(const TNode *Node) {
  if (Node->Parent == NULL) {
    return Root;
  }
  if (Node->LChild == NULL && Node->RChild == NULL) {
    return Leaf;
  }
  return InternalNode;
}

void Output(const vector<TNode *> &Nodes) {
  for (unsigned int i = 0; i < Nodes.size(); i++) {
    cout << "node " << Nodes[i]->Id << ": ";
    if (Nodes[i]->Parent) {
      cout << "parent = " << Nodes[i]->Parent->Id << ", ";
    } else {
      cout << "parent = -1, ";
    }
    cout << "sibling = " << GetSibling(Nodes[i]) << ", ";
    cout << "degree = " << GetDegree(Nodes[i]) << ", ";
    cout << "depth = " << GetDepth(Nodes[i]) << ", ";
    cout << "height = " << GetHeight(Nodes[i]) << ", ";
    if (GetType(Nodes[i]) == Root) {
      cout << "root";
    } else if (GetType(Nodes[i]) == InternalNode) {
      cout << "internal node";
    } else {
      cout << "leaf";
    }
    cout << endl;
  }
}

void Delete(vector<TNode *> &Nodes) {
  int n = Nodes.size();
  for (int i = 0; i < n; i++) {
    delete Nodes[i];
  }
}

int main() {
  vector<TNode *> Nodes = Input();
  Output(Nodes);
  //	Delete(Nodes);
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct TNode {
  int Id;
  TNode *Parent;
  TNode *NextSibling;
  TNode *FirstChild;
};

enum TNodeType { Root, InternalNode, Leaf };

void Create(int Id, vector<TNode *> &Nodes) {
  TNode *Node = new TNode;
  *Node = {Id, NULL, NULL, NULL};
  Nodes[Id] = Node;
}

void CreateRelation(TNode *ParentNode, TNode *LastChild, TNode *ChildNode) {
  if (LastChild == NULL) {
    ParentNode->FirstChild = ChildNode;
    ChildNode->Parent = ParentNode;
  } else {
    LastChild->NextSibling = ChildNode;
    ChildNode->Parent = LastChild->Parent;
  }
}

vector<TNode *> Input() {
  int n;
  cin >> n;
  vector<TNode *> Nodes(n, NULL);
  for (int i = 0; i < n; i++) {
    int Id;
    cin >> Id;
    if (Nodes[Id] == NULL) {
      Create(Id, Nodes);
    }
    int ChildCount;
    cin >> ChildCount;
    TNode *LastChild = NULL;
    for (int j = 0; j < ChildCount; j++) {
      int ChildId;
      cin >> ChildId;
      if (Nodes[ChildId] == NULL) {
        Create(ChildId, Nodes);
      }
      CreateRelation(Nodes[Id], LastChild, Nodes[ChildId]);
      LastChild = Nodes[ChildId];
    }
  }
  return Nodes;
}

int GetDepth(const TNode *Node) {
  int depth = 0;
  while (Node->Parent) {
    depth++;
    Node = Node->Parent;
  }
  return depth;
}

TNodeType GetType(const TNode *Node) {
  if (Node->Parent == NULL) {
    return Root;
  } else if (Node->FirstChild == NULL) {
    return Leaf;
  } else {
    return InternalNode;
  }
}

void OutputChildren(const TNode *Node) {
  if (Node->FirstChild == NULL) {
    return;
  }
  Node = Node->FirstChild;
  cout << Node->Id;
  while (Node->NextSibling) {
    Node = Node->NextSibling;
    cout << ", " << Node->Id;
  }
}

void Output(const vector<TNode *> &Nodes) {
  for (unsigned int i = 0; i < Nodes.size(); i++) {
    cout << "node " << Nodes[i]->Id << ": ";
    if (Nodes[i]->Parent) {
      cout << "parent = " << Nodes[i]->Parent->Id << ", ";
    } else {
      cout << "parent = -1, ";
    }
    cout << "depth = " << GetDepth(Nodes[i]) << ", ";
    if (GetType(Nodes[i]) == Root) {
      cout << "root, ";
    } else if (GetType(Nodes[i]) == InternalNode) {
      cout << "internal node, ";
    } else {
      cout << "leaf, ";
    }
    cout << "[";
    OutputChildren(Nodes[i]);
    cout << "]" << endl;
  }
}

int main() {
  vector<TNode *> Nodes = Input();
  Output(Nodes);
  return 0;
}