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

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

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

//???????????????????????????TNode*???????????????(vector??????????????¢?????????????????????)
class TTable {
private:
  vector<TNode *> Nodes;
  void Create(int Id, vector<TNode *> &Nodes) {
    if (Nodes[Id] == NULL) {
      TNode *Node;
      Node = new TNode;
      *Node = {Id, NULL, NULL, NULL};
      Nodes[Id] = Node;
    }
  }

public:
  // TNode*?????????&?????????????????????????????????
  //????????????????????§delete????????????????????????????????????????????????????????¨?????????????????????????????????????????????
  //???????????????????????????Id??¨TNode*???????????????????????¨??§????????????????????????????????¨????????????????????????????????????
  TNode *operator[](int n) {
    Create(n, Nodes);
    return Nodes[n];
  }
  TNode *operator[](int n) const { return Nodes[n]; }
  TTable(int n) {
    //????´??????°???????????????
    Nodes.resize(n, NULL);
  }
  ~TTable() {
    int n = Nodes.size();
    for (int i = 0; i < n; i++) {
      delete Nodes[i];
    }
  }
  int size() const { return Nodes.size(); }
};

enum TNodeType { Root, InternalNode, Leaf };

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

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

void Input(TTable &Table) {
  for (int i = 0; i < Table.size(); i++) {
    int Id;
    cin >> Id;
    int left;
    cin >> left;
    if (left != -1) {
      CreateLeftRelation(Table[Id], Table[left]);
    }
    int right;
    cin >> right;
    if (right != -1) {
      CreateRightRelation(Table[Id], Table[right]);
    }
  }
}

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 TTable &Table) {
  for (unsigned int i = 0; i < Table.size(); i++) {
    cout << "node " << Table[i]->Id << ": ";
    if (Table[i]->Parent) {
      cout << "parent = " << Table[i]->Parent->Id << ", ";
    } else {
      cout << "parent = -1, ";
    }
    cout << "sibling = " << GetSibling(Table[i]) << ", ";
    cout << "degree = " << GetDegree(Table[i]) << ", ";
    cout << "depth = " << GetDepth(Table[i]) << ", ";
    cout << "height = " << GetHeight(Table[i]) << ", ";
    if (GetType(Table[i]) == Root) {
      cout << "root";
    } else if (GetType(Table[i]) == InternalNode) {
      cout << "internal node";
    } else {
      cout << "leaf";
    }
    cout << endl;
  }
}

int main() {
  int n;
  cin >> n;
  if (n == 1) {
    cout << "node 0: parent = -1, sibling = -1, degree = 0, depth = 0, height "
            "= 0, root"
         << endl;
    return 0;
  }
  TTable Table(n);
  Input(Table);
  Output(Table);
  return 0;
}

Error: 0

Fixed submission:

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

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

//???????????????????????????TNode*???????????????(vector??????????????¢?????????????????????)
class TTable {
private:
  vector<TNode *> Nodes;
  void Create(int Id, vector<TNode *> &Nodes) {
    if (Nodes[Id] == NULL) {
      TNode *Node;
      Node = new TNode;
      *Node = {Id, NULL, NULL, NULL};
      Nodes[Id] = Node;
    }
  }

public:
  // TNode*?????????&?????????????????????????????????
  //????????????????????§delete????????????????????????????????????????????????????????¨?????????????????????????????????????????????
  //???????????????????????????Id??¨TNode*???????????????????????¨??§????????????????????????????????¨????????????????????????????????????
  TNode *operator[](int n) {
    Create(n, Nodes);
    return Nodes[n];
  }
  TNode *operator[](int n) const { return Nodes[n]; }
  TTable(int n) {
    //????´??????°???????????????
    Nodes.resize(n, NULL);
  }
  ~TTable() {
    int n = Nodes.size();
    for (int i = 0; i < n; i++) {
      delete Nodes[i];
    }
  }
  int size() const { return Nodes.size(); }
};

enum TNodeType { Root, InternalNode, Leaf };

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

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

void Input(TTable &Table) {
  for (int i = 0; i < Table.size(); i++) {
    int Id;
    cin >> Id;
    int left;
    cin >> left;
    if (left != -1) {
      CreateLeftRelation(Table[Id], Table[left]);
    }
    int right;
    cin >> right;
    if (right != -1) {
      CreateRightRelation(Table[Id], Table[right]);
    }
  }
}

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 TTable &Table) {
  for (unsigned int i = 0; i < Table.size(); i++) {
    cout << "node " << Table[i]->Id << ": ";
    if (Table[i]->Parent) {
      cout << "parent = " << Table[i]->Parent->Id << ", ";
    } else {
      cout << "parent = -1, ";
    }
    cout << "sibling = " << GetSibling(Table[i]) << ", ";
    cout << "degree = " << GetDegree(Table[i]) << ", ";
    cout << "depth = " << GetDepth(Table[i]) << ", ";
    cout << "height = " << GetHeight(Table[i]) << ", ";
    if (GetType(Table[i]) == Root) {
      cout << "root";
    } else if (GetType(Table[i]) == InternalNode) {
      cout << "internal node";
    } else {
      cout << "leaf";
    }
    cout << endl;
  }
}

int main() {
  int n;
  cin >> n;
  TTable Table(n);
  Input(Table);
  Output(Table);
  return 0;
}