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

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

struct Vertex {
  int parent;
  int depth;
  int sibling;
  int height;
};

void treeMake(int vertex, int depth, vector<vector<int>> &T,
              vector<Vertex> &V) {
  if (depth == 0) {
    V[vertex].parent = -1;
    V[vertex].sibling = -1;
  }
  V[vertex].depth = depth;
  if (T[vertex][0] == -1 && T[vertex][1] == -1) {
    return;
  }
  int i;

  if (T[vertex][0] != -1) {
    V[T[vertex][0]].parent = vertex;
    V[T[vertex][0]].sibling = T[vertex][1];
    treeMake(T[vertex][0], depth + 1, T, V);
  }

  if (T[vertex][1] != -1) {
    V[T[vertex][1]].parent = vertex;
    V[T[vertex][1]].sibling = T[vertex][0];
    treeMake(T[vertex][1], depth + 1, T, V);
  }
  /*for(i=0;i<T[vertex].size();i++){
          V[T[vertex][i]].parent=vertex;
          treeMake(T[vertex][i],depth+1,T,V);

  }*/
}

int main() {
  int n, i, j, k, d, e;
  cin >> n;

  vector<vector<int>> T(n);
  vector<int> check(n, 0);

  for (i = 0; i < n; i++) {
    cin >> k;
    for (j = 0; j < 2; j++) {
      cin >> e;
      T[k].push_back(e);
      if (e != -1)
        check[e] = 1;
    }
  }

  int r;
  for (i = 0; i < n; i++) {
    if (check[i] == 0) {
      r = i;
      break;
    }
  }

  vector<Vertex> V(n);

  treeMake(r, 0, T, V);

  // height????¨????
  for (i = 0; i < n; i++) {
    if (T[i][0] == -1 && T[i][1] == -1) {
      int j = i;
      int count = 0;
      V[j].height = count;
      while (1) {
        j = V[j].parent;
        count++;
        if (j == -1)
          break;
        if (V[j].height < count) {
          V[j].height = count;
        }
      }
    }
  }

  for (i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << V[i].parent
         << ", sibling = " << V[i].sibling << ", degree = ";
    if (T[i][0] == -1 && T[i][1] == -1) {
      cout << 0;
    } else if (T[i][0] == -1 || T[i][1] == -1) {
      cout << 1;
    } else {
      cout << 2;
    }
    cout << ", depth = " << V[i].depth << ", height = " << V[i].height;
    if (V[i].parent == -1) {
      cout << ", root";
    } else if (T[i][0] == -1 && T[i][1] == -1) {
      cout << ", leaf";
    } else {
      cout << ", internal node";
    }
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

struct Vertex {
  int parent;
  int depth;
  int sibling;
  int height;
};

void treeMake(int vertex, int depth, vector<vector<int>> &T,
              vector<Vertex> &V) {
  if (depth == 0) {
    V[vertex].parent = -1;
    V[vertex].sibling = -1;
  }
  V[vertex].depth = depth;
  if (T[vertex][0] == -1 && T[vertex][1] == -1) {
    return;
  }
  int i;

  V[T[vertex][0]].parent = vertex;
  V[T[vertex][0]].sibling = T[vertex][1];
  if (T[vertex][0] != -1)
    treeMake(T[vertex][0], depth + 1, T, V);

  V[T[vertex][1]].parent = vertex;
  V[T[vertex][1]].sibling = T[vertex][0];
  if (T[vertex][1] != -1)
    treeMake(T[vertex][1], depth + 1, T, V);
  /*for(i=0;i<T[vertex].size();i++){
          V[T[vertex][i]].parent=vertex;
          treeMake(T[vertex][i],depth+1,T,V);

  }*/
}

int main() {
  int n, i, j, k, d, e;
  cin >> n;

  vector<vector<int>> T(n);
  vector<int> check(n, 0);

  for (i = 0; i < n; i++) {
    cin >> k;
    for (j = 0; j < 2; j++) {
      cin >> e;
      T[k].push_back(e);
      if (e != -1)
        check[e] = 1;
    }
  }

  int r;
  for (i = 0; i < n; i++) {
    if (check[i] == 0) {
      r = i;
      break;
    }
  }

  vector<Vertex> V(n);

  treeMake(r, 0, T, V);

  // height????¨????
  for (i = 0; i < n; i++) {
    if (T[i][0] == -1 && T[i][1] == -1) {
      int j = i;
      int count = 0;
      V[j].height = count;
      while (1) {
        j = V[j].parent;
        count++;
        if (j == -1)
          break;
        if (V[j].height < count) {
          V[j].height = count;
        }
      }
    }
  }

  for (i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << V[i].parent
         << ", sibling = " << V[i].sibling << ", degree = ";
    if (T[i][0] == -1 && T[i][1] == -1) {
      cout << 0;
    } else if (T[i][0] == -1 || T[i][1] == -1) {
      cout << 1;
    } else {
      cout << 2;
    }
    cout << ", depth = " << V[i].depth << ", height = " << V[i].height;
    if (V[i].parent == -1) {
      cout << ", root";
    } else if (T[i][0] == -1 && T[i][1] == -1) {
      cout << ", leaf";
    } else {
      cout << ", internal node";
    }
    cout << endl;
  }
  return 0;
}