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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 10000;
struct node {
  int l, r, p, s, deg, dep, h;
};
int n, v, l1, r1, tmp, root;
int vis[N] = {0};
node tree[N];

int build(int k, int depth) {
  if ((tree[k].l == -1) && (tree[k].r == -1)) {
    tree[k].deg = 0;
    tree[k].dep = depth;
    return tree[k].h = 0;
  } else if ((tree[k].l != -1) && (tree[k].r != -1)) {
    tree[tree[k].l].p = k;
    tree[tree[k].r].p = k;
    tree[tree[k].l].s = tree[k].r;
    tree[tree[k].r].s = tree[k].l;
    tree[k].deg = 2;
    tree[k].dep = depth;
    return tree[k].h =
               max(build(tree[k].l, depth + 1), build(tree[k].r, depth + 1)) +
               1;
  } else if (tree[k].l != -1) {
    tree[tree[k].l].p = k;

    tree[tree[k].l].s = -1;

    tree[k].deg = 1;
    tree[k].dep = depth;
    return tree[k].h = build(tree[k].l, depth + 1) + 1;
  } else if (tree[k].r != -1) {

    tree[tree[k].r].p = k;

    tree[tree[k].r].s = -1;

    tree[k].deg = 1;
    tree[k].dep = depth;
    return tree[k].h = build(tree[k].r, depth + 1) + 1;
  }
}
int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> v >> l1 >> r1;
    tree[v].l = l1;
    tree[v].r = r1;
    if (tree[v].l != -1)
      vis[tree[v].l] = 1;
    if (tree[v].r != -1)
      vis[tree[v].r] = 1;
  }
  for (int i = 0; i < n; i++) {
    if (vis[i] == 0) {
      root = i;
      break;
    }
  }
  build(root, 0);
  tree[root].p = -1;
  tree[root].s = -1;
  for (int i = 0; i < n; i++) {
    printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, "
           "height = %d, ",
           i, tree[i].p, tree[i].s, tree[i].deg, tree[i].dep, tree[i].h);
    if (i == root)
      printf("root");
    else if (tree[i].l == -1 && tree[i].r == -1)
      printf("leaf");
    else
      printf("internal node");
    printf("\\n");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
struct kz {
  int parent = -1;
  vector<int> degree;
  int depth;
  int sibling = -1;
  int height;
  char leave[16];
  int fuck = 0;
} Tree[10000];
void depth(int x) {
  if (Tree[x].degree[0] == -1)
    return;
  for (int i = 0; i < 2; i++) {
    Tree[Tree[x].degree[i]].depth = Tree[x].depth + 1;

    depth(Tree[x].degree[i]);
  }
}
int height(int x) {
  int h1 = 0, h2 = 0;
  if (Tree[x].degree[0] != -1)
    h1 = height(Tree[x].degree[0]) + 1;
  if (Tree[x].degree[1] != -1)
    h2 = height(Tree[x].degree[1]) + 1;
  Tree[x].height = max(h1, h2);
  return Tree[x].height;
}
int main() {
  int n, m;
  scanf("%d", &n);
  int l, r;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &m, &l, &r);

    Tree[l].parent = m;
    Tree[l].sibling = r;
    Tree[r].sibling = l;
    Tree[r].parent = m;
    Tree[m].degree.push_back(l);
    Tree[m].degree.push_back(r);
    strcpy(Tree[m].leave, "internal node");
    Tree[m].fuck = 2;

    if (l == -1 && r == -1) {
      strcpy(Tree[m].leave, "leaf");
      Tree[m].fuck = 0;
    }
    if (l == -1) {
      if (r != -1)
        Tree[m].fuck = 1;
    }
    if (r == -1) {
      if (l != -1)
        Tree[m].fuck = 1;
    }
  }
  int s = 0;
  while (Tree[s].parent != -1)
    s = Tree[s].parent;
  Tree[s].depth = 0;
  strcpy(Tree[s].leave, "root");
  depth(s);
  height(s);
  for (int i = 0; i < n; i++)
    printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, "
           "height = %d, %s\\n",
           i, Tree[i].parent, Tree[i].sibling, Tree[i].fuck, Tree[i].depth,
           Tree[i].height, Tree[i].leave);

  return 0;
}