Problem p02280 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

struct node {
  node() {
    parent = -1;
    depth = 0;
    s = -1;
  }
  void set(const int c1, const int c2) {
    if (c1 != -1 && c2 != -1) {
      leaf.resize(2);
      leaf[0] = c1;
      leaf[1] = c2;
    } else if (c1 != -1 || c2 != -1) {
      leaf.resize(1);
      leaf[0] = c1;
    } else {
      leaf.resize(0);
    }
  }
  int s;
  int height;
  int parent;
  int depth;
  vector<int> leaf;
};

int dp(int id, int d);
node dnode[100000];
int main() {
  int N, id, c1, c2;
  queue<int> que;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> id >> c1 >> c2;
    dnode[id].set(c1, c2);
  }
  //*
  for (int i = 0; i < N; i++) {
    if (dnode[i].leaf.size()) {
      for (int j = 0; j < dnode[i].leaf.size(); j++) {
        dnode[dnode[i].leaf[j]].parent = i;
        if (dnode[i].leaf.size() == 2) {
          dnode[dnode[i].leaf[j]].s = dnode[i].leaf[1 - j];
        }
      }
    }
  } //*/
  for (int i = 0; i < N; i++) {
    if (dnode[i].parent == -1) {
      /*
      for(que.push(i);!que.empty();que.pop()){
              for(int j=0;j<dnode[que.front()].leaf.size();j++){
                      dnode[dnode[que.front()].leaf[j]].depth=dnode[que.front()].depth+1;
                      if(dnode[dnode[que.front()].leaf[j]].leaf.size()){
                              que.push(dnode[que.front()].leaf[j]);
                      }
              }
      }//*/
      dp(i, 0);
      break;
    }
  } //*/
  for (int i = 0; i < N; i++) {
    cout << "node " << i << ": parent = " << dnode[i].parent
         << ", sibling = " << dnode[i].s
         << ", degree = " << dnode[i].leaf.size()
         << ", depth = " << dnode[i].depth << ", height = " << dnode[i].height;
    if (dnode[i].parent == -1) {
      cout << ", root";
    } else if (dnode[i].leaf.size()) {
      cout << ", internal node";
    } else {
      cout << ", leaf";
    }
    cout << endl;
  }
}
int dp(int id, int d) {
  dnode[id].depth = d;
  if (dnode[id].leaf.size()) {
    int h1, h2;
    h1 = dp(dnode[id].leaf[0], d + 1);
    if (dnode[id].leaf.size() == 2) {
      h2 = dp(dnode[id].leaf[1], d + 1);
    } else {
      h2 = 0;
    }
    dnode[id].height = h1 > h2 ? h1 : h2;
    return dnode[id].height + 1;
  } else {
    dnode[id].height = 0;
    return 1;
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

struct node {
  node() {
    parent = -1;
    depth = 0;
    s = -1;
  }
  void set(const int c1, const int c2) {
    if (c1 != -1 && c2 != -1) {
      leaf.resize(2);
      leaf[0] = c1;
      leaf[1] = c2;
    } else if (c1 != -1 || c2 != -1) {
      leaf.resize(1);
      if (c1 != -1) {
        leaf[0] = c1;
      } else {
        leaf[0] = c2;
      }
    } else {
      leaf.resize(0);
    }
  }
  int s;
  int height;
  int parent;
  int depth;
  vector<int> leaf;
};

int dp(int id, int d);
node dnode[100000];
int main() {
  int N, id, c1, c2;
  queue<int> que;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> id >> c1 >> c2;
    dnode[id].set(c1, c2);
  }
  //*
  for (int i = 0; i < N; i++) {
    if (dnode[i].leaf.size()) {
      for (int j = 0; j < dnode[i].leaf.size(); j++) {
        dnode[dnode[i].leaf[j]].parent = i;
        if (dnode[i].leaf.size() == 2) {
          dnode[dnode[i].leaf[j]].s = dnode[i].leaf[1 - j];
        }
      }
    }
  } //*/
  for (int i = 0; i < N; i++) {
    if (dnode[i].parent == -1) {
      /*
      for(que.push(i);!que.empty();que.pop()){
              for(int j=0;j<dnode[que.front()].leaf.size();j++){
                      dnode[dnode[que.front()].leaf[j]].depth=dnode[que.front()].depth+1;
                      if(dnode[dnode[que.front()].leaf[j]].leaf.size()){
                              que.push(dnode[que.front()].leaf[j]);
                      }
              }
      }//*/
      dp(i, 0);
      break;
    }
  } //*/
  for (int i = 0; i < N; i++) {
    cout << "node " << i << ": parent = " << dnode[i].parent
         << ", sibling = " << dnode[i].s
         << ", degree = " << dnode[i].leaf.size()
         << ", depth = " << dnode[i].depth << ", height = " << dnode[i].height;
    if (dnode[i].parent == -1) {
      cout << ", root";
    } else if (dnode[i].leaf.size()) {
      cout << ", internal node";
    } else {
      cout << ", leaf";
    }
    cout << endl;
  }
}
int dp(int id, int d) {
  dnode[id].depth = d;
  if (dnode[id].leaf.size()) {
    int h1, h2;
    h1 = dp(dnode[id].leaf[0], d + 1);
    if (dnode[id].leaf.size() == 2) {
      h2 = dp(dnode[id].leaf[1], d + 1);
    } else {
      h2 = 0;
    }
    dnode[id].height = h1 > h2 ? h1 : h2;
    return dnode[id].height + 1;
  } else {
    dnode[id].height = 0;
    return 1;
  }
}