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

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

constexpr int nullNum = -1;

class Node {
public:
  Node() : id(nullNum), parent(nullptr), left(nullptr), right(nullptr) {}

  void SetID(int id_) { id = id_; }

  void SetParent(Node *prt) { parent = prt; }

  void SetLeft(Node *l) { left = l; }

  void SetRight(Node *r) { right = r; }

  int &ID() { return id; }

  int Degree() {
    int deg = 0;
    deg += (left) ? 1 : 0;
    deg += (right) ? 1 : 0;
    return deg;
  }

  int Depth() {
    int depth = 0;
    Node *ptr = Parent();
    while (ptr) {
      ++depth;
      ptr = ptr->Parent();
    }
    return depth;
  }

  int Height(Node *const node) {
    if (!node || (!node->Left() && !node->Right())) {
      return 0;
    } else {
      return 1 + std::max(Height(node->Left()), Height(node->Right()));
    }
  }

  Node *Parent() { return parent; }

  Node *Left() { return left; }

  Node *Right() { return right; }

  Node *Sibling() {
    if (parent == nullptr) {
      return nullptr;
    } else if (this == parent->Left()) {
      return parent->Right();
    } else {
      return parent->Left();
    }
  }

  const char *CategoryName() {
    if (!parent) {
      return "root";
    } else if (!left && !right) {
      return "leaf";
    }
    return "internal node";
  }

private:
  int id;
  Node *parent;
  Node *left;
  Node *right;
};

int main() {
  int n;
  std::cin.tie(0);
  std::cin >> n;

  std::vector<Node> tree(n);

  for (auto &elem : tree) {
    elem = Node();
  }

  int id;
  int lid;
  int rid;
  for (int i = 0; i < n; ++i) {
    std::cin >> id >> lid >> rid;
    tree[id].SetID(id);
    if (lid != nullNum) {
      tree[id].SetLeft(&tree[lid]);
      tree[lid].SetParent(&tree[id]);
    }
    if (rid != nullNum) {
      tree[id].SetRight(&tree[rid]);
      tree[rid].SetParent(&tree[id]);
    }
  }

  for (int i = 0; i < n; ++i) {
    std::cout << "node " << tree[i].ID();
    std::cout << ": parent = "
              << ((!tree[i].Parent()) ? -1 : tree[i].Parent()->ID());
    std::cout << ", sibling = "
              << ((!tree[i].Sibling()) ? -1 : tree[i].Sibling()->ID());
    std::cout << ", degree = " << tree[i].Degree();
    std::cout << ", depth = " << tree[i].Depth();
    std::cout << ", height = " << tree[i].Height(&tree[i]);
    std::cout << ", " << tree[i].CategoryName() << "\\n";
  }
}


Error: 0

Fixed submission:

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

constexpr int nullNum = -1;

class Node {
public:
  Node() : id(nullNum), parent(nullptr), left(nullptr), right(nullptr) {}

  void SetID(int id_) { id = id_; }

  void SetParent(Node *prt) { parent = prt; }

  void SetLeft(Node *l) { left = l; }

  void SetRight(Node *r) { right = r; }

  int &ID() { return id; }

  int Degree() {
    int deg = 0;
    deg += (left) ? 1 : 0;
    deg += (right) ? 1 : 0;
    return deg;
  }

  int Depth() {
    int depth = 0;
    Node *ptr = Parent();
    while (ptr) {
      ++depth;
      ptr = ptr->Parent();
    }
    return depth;
  }

  int Height(Node *const node) {
    if (!node->Left() && !node->Right()) {
      return 0;
    } else {
      return 1 + std::max(Height(node->Left()), Height(node->Right()));
    }
  }

  Node *Parent() { return parent; }

  Node *Left() { return left; }

  Node *Right() { return right; }

  Node *Sibling() {
    if (parent == nullptr) {
      return nullptr;
    } else if (this == parent->Left()) {
      return parent->Right();
    } else {
      return parent->Left();
    }
  }

  const char *CategoryName() {
    if (!parent) {
      return "root";
    } else if (!left && !right) {
      return "leaf";
    }
    return "internal node";
  }

private:
  int id;
  Node *parent;
  Node *left;
  Node *right;
};

int main() {
  int n;
  std::cin.tie(0);
  std::cin >> n;

  std::vector<Node> tree(n);

  for (auto &elem : tree) {
    elem = Node();
  }

  int id;
  int lid;
  int rid;

  for (int i = 0; i < n; ++i) {
    std::cin >> id >> lid >> rid;
    tree[id].SetID(id);
    if (lid != nullNum) {
      tree[id].SetLeft(&tree[lid]);
      tree[lid].SetParent(&tree[id]);
    }
    if (rid != nullNum) {
      tree[id].SetRight(&tree[rid]);
      tree[rid].SetParent(&tree[id]);
    }
  }

  for (int i = 0; i < n; ++i) {
    std::cout << "node " << tree[i].ID() << ": parent = "
              << ((!tree[i].Parent()) ? -1 : tree[i].Parent()->ID())
              << ", sibling = "
              << ((!tree[i].Sibling()) ? -1 : tree[i].Sibling()->ID())
              << ", degree = " << tree[i].Degree()
              << ", depth = " << tree[i].Depth()
              << ", height = " << tree[i].Height(&tree[i]) << ", "
              << tree[i].CategoryName() << "\\n";
  }
}