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

#include <bits/stdc++.h>
#define NUL -1
using namespace std;
struct Node {
  int par, lef, rig, dep, sib, deg;
};
Node T[26];
bool flg[26] = {0};
void func(int node, int parent, int depth);
int heigth(int NODE);
int main(void) {
  int n;
  int i, j;
  int u, l, r;
  int cnt;
  cin >> n;
  for (i = 0; i < n; i++)
    T[i].par = T[i].lef = T[i].rig = T[i].dep = T[i].sib = T[i].deg = NUL;
  for (i = 0; i < n; i++) {
    cin >> u >> l >> r;
    T[u].lef = l;
    T[u].rig = r;
    flg[l] = 1;
    flg[r] = 1;
    cnt = 0;
    if (l != -1) {
      T[l].sib = r;
      cnt++;
    }
    if (r != -1) {
      T[r].sib = l;
      cnt++;
    }
    T[u].deg = cnt;
  }
  for (i = 0; i < n; i++) {
    if (!flg[i]) {
      func(i, -1, 0);
    }
  }

  for (i = 0; i < n; i++) {
    // cout<<"Check"<<endl;
    cout << "node " << i << ": parent = " << T[i].par
         << ", sibling = " << T[i].sib << ", degree = ";
    cout << T[i].deg << ", depth = " << T[i].dep << ", ";
    cout << "height = " << heigth(i) << ", ";
    if (T[i].deg == 0) {
      if (T[i].par == NUL) {
        cout << "root" << endl;
      } else {
        cout << "leaf" << endl;
      }
    } else {
      if (T[i].par == NUL) {
        cout << "root" << endl;
      } else {
        cout << "internal node" << endl;
      }
    }
  }
  return 0;
}
void func(int node, int parent, int depth) {
  int i, j;
  // cout<<parent<<endl;
  T[node].dep = depth;
  T[node].par = parent;
  if (T[node].lef != NUL) {
    func(T[node].lef, node, depth + 1);
  }
  if (T[node].rig != NUL) {
    func(T[node].rig, node, depth + 1);
  }
}
int heigth(int NODE) {
  int hr = 0, hl = 0;
  // cout<<T[NODE].lef<<endl;
  // cout<<T[NODE].rig<<endl;
  // cout<<"Check1"<<endl;
  if (T[NODE].lef != -1)
    hl = heigth(T[NODE].lef) + 1;
  if (T[NODE].rig != -1)
    hr = heigth(T[NODE].rig) + 1;
  // cout<<hr<<endl;
  // cout<<hl<<endl;
  return max(hr, hl);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define NUL -1
using namespace std;
struct Node {
  int par, lef, rig, dep, sib, deg;
};
Node T[26];
bool flg[26] = {0};
void func(int node, int parent, int depth);
int heigth(int NODE);
int main(void) {
  int n;
  int i, j;
  int u, l, r;
  int both;
  int cnt;
  cin >> n;
  for (i = 0; i < n; i++)
    T[i].par = T[i].lef = T[i].rig = T[i].dep = T[i].sib = T[i].deg = NUL;
  for (i = 0; i < n; i++) {
    cin >> u >> l >> r;
    T[i].lef = l;
    T[i].rig = r;
    flg[l] = 1;
    flg[r] = 1;
    if (l != -1)
      T[l].sib = r;
    if (r != -1)
      T[r].sib = l;
    if (l + r == -2)
      T[u].deg = 0;
    else
      T[u].deg = 2;
  }
  for (i = 0; i < n; i++) {
    if (!flg[i]) {
      func(i, -1, 0);
    }
  }
  for (i = 0; i < n; i++) {
    cout << "node " << i << ": parent = " << T[i].par
         << ", sibling = " << T[i].sib << ", degree = " << T[i].deg
         << ", depth = " << T[i].dep << ",";
    both = i;
    cout << "heigth = " << heigth(i) << ", ";
    if (T[i].deg == 0) {
      cout << "leaf" << endl;
    } else {
      if (T[i].par == NUL) {
        cout << "root" << endl;
      } else {
        cout << "internal node" << endl;
      }
    }
  }
  return 0;
}
void func(int node, int parent, int depth) {
  int i, j;
  // cout<<parent<<endl;
  T[node].dep = depth;
  T[node].par = parent;
  if (T[node].lef != NUL) {
    func(T[node].lef, node, depth + 1);
  }
  if (T[node].rig != NUL) {
    func(T[node].rig, node, depth + 1);
  }
}
int heigth(int NODE) {
  int hr = 0, hl = 0;
  // cout<<T[NODE].lef<<endl;
  // cout<<T[NODE].rig<<endl;
  if (T[NODE].lef != -1)
    hl = heigth(T[NODE].lef) + 1;
  if (T[NODE].rig != -1)
    hr = heigth(T[NODE].rig) + 1;
  // cout<<hr<<endl;
  // cout<<hl<<endl;
  return max(hr, hl);
}