Problem:
Binary Tree

A rooted binary tree is a tree with a root node in which every node has at most two children.

Your task is to write a program which reads a rooted binary tree T and prints the following information for each node u of T:

node ID of u

parent of u

sibling of u

the number of children of u

depth of u

height of u

node type (root, internal node or leaf)

If two nodes have the same parent, they are siblings. Here, if u and v have the same parent, we say u is a sibling of v (vice versa).

The height of a node in a tree is the number of edges on the longest simple downward path from the node to a leaf.

Here, the given binary tree consists of n nodes and evey node has a unique ID from 0 to n-1.

Input

The first line of the input includes an integer n, the number of nodes of the tree.

In the next n lines, the information of each node is given in the following format:

id left right

id is the node ID, left is ID of the left child and right is ID of the right child. If the node does not have the left (right) child, the left(right) is indicated by -1.

Output

Print the information of each node in the following format:

node id: parent = p, sibling = s, degree = deg, depth = dep, height = h, type

p is ID of its parent. If the node does not have a parent, print -1.

s is ID of its sibling. If the node does not have a sibling, print -1.

deg, dep and h are the number of children, depth and height of the node respectively.

type is a type of nodes represented by a string (root, internal node or leaf. If the root can be considered as a leaf or an internal node, print root.

Please follow the format presented in a sample output below.

Constraints

1 ≤ n ≤ 25

Sample Input 1

9
0 1 4
1 2 3
2 -1 -1
3 -1 -1
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Sample Output 1

node 0: parent = -1, sibling = -1, degree = 2, depth = 0, height = 3, root
node 1: parent = 0, sibling = 4, degree = 2, depth = 1, height = 1, internal node
node 2: parent = 1, sibling = 3, degree = 0, depth = 2, height = 0, leaf
node 3: parent = 1, sibling = 2, degree = 0, depth = 2, height = 0, leaf
node 4: parent = 0, sibling = 1, degree = 2, depth = 1, height = 2, internal node
node 5: parent = 4, sibling = 8, degree = 2, depth = 2, height = 1, internal node
node 6: parent = 5, sibling = 7, degree = 0, depth = 3, height = 0, leaf
node 7: parent = 5, sibling = 6, degree = 0, depth = 3, height = 0, leaf
node 8: parent = 4, sibling = 5, degree = 0, depth = 2, height = 0, leaf

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Solution in JAVA (judge verdict: Accepted)

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.TreeSet;
 
public class Main {
  public static void main(String[] args) {
    InputReader sc = new InputReader(System.in);
 
    int n = sc.nextInt();
    int[] parentlist = new int[n];
    int[] siblinglist = new int[n];
    int[] degreelist = new int[n];
    int[] depthlist = new int[n];
    int[] heightlist = new int[n];
    int[] leftlist = new int[n];
    int[] rightlist = new int[n];
    boolean[] rootcheck = new boolean[n];
    boolean[] leafcheck = new boolean[n];
    for(int i = 0; i < n; i++){
      rootcheck[i] = true;
      leafcheck[i] = false;
    }
    for(int i = 0; i < n; i++){
      int id = sc.nextInt();
      int left = sc.nextInt();
      int right = sc.nextInt();
      if(left < 0 && right < 0){
        leafcheck[id] = true;
        degreelist[id] = 0;
      }else if(left >= 0 && right >= 0){
        degreelist[id] = 2;
        siblinglist[left] = right;
        siblinglist[right] = left;
        parentlist[left] = id;
        parentlist[right] = id;
        rootcheck[left] = false;
        rootcheck[right] = false;
        leftlist[id] = left;
        rightlist[id] = right;
      }else if(left >= 0){
        degreelist[id] = 1;
        siblinglist[left] = -1;
        parentlist[left] = id;
        rootcheck[left] = false;
        leftlist[id] = left;
        rightlist[id] = -1;
      }else{
        degreelist[id] = 1;
        siblinglist[right] = -1;
        parentlist[right] = id;
        rootcheck[right] = false;
        rightlist[id] = right;
        leftlist[id] = -1;
      }
    }

    int rootind = -1;
    for(int i = 0; i < n; i++){
      if(rootcheck[i]){
        rootind = i;
        break;
      }
    }
    parentlist[rootind] = -1;
    siblinglist[rootind] = -1;

    LinkedList<Integer> indexqueue = new LinkedList<Integer>();
    LinkedList<Integer> depthqueue = new LinkedList<Integer>();
    indexqueue.add(rootind);
    depthqueue.add(0);
    depthlist[rootind] = 0;

    while(!indexqueue.isEmpty()){
      int nowindex = indexqueue.poll();
      int nowdepth = depthqueue.poll();
      if(degreelist[nowindex] == 0){continue;}
      if(leftlist[nowindex] >= 0){
        depthlist[leftlist[nowindex]] = nowdepth + 1;
        indexqueue.add(leftlist[nowindex]);
        depthqueue.add(nowdepth+1);        
      }
      if(rightlist[nowindex] >= 0){
        depthlist[rightlist[nowindex]] = nowdepth + 1;
        indexqueue.add(rightlist[nowindex]);
        depthqueue.add(nowdepth+1);
      }
    }


    for(int i = 0; i < n; i++){
      heightlist[i] = 0;
    }
    indexqueue.clear();
    LinkedList<Integer> heightqueue = new LinkedList<Integer>();
    for(int i = 0; i < n; i++){
      if(leafcheck[i]){
        indexqueue.add(i);
        heightqueue.add(0);
      }
    }
    while(!heightqueue.isEmpty()){
      int nowindex = indexqueue.poll();
      int nowheight = heightqueue.poll();
      if(parentlist[nowindex] < 0){
        continue;
      }
      if(heightlist[parentlist[nowindex]] < nowheight + 1){
        heightlist[parentlist[nowindex]] = nowheight + 1;
        indexqueue.add(parentlist[nowindex]);
        heightqueue.add(nowheight + 1);
      }
    }

    for(int i = 0; i < n; i++){
      System.out.print("node ");
      System.out.print(i);
      System.out.print(": parent = ");
      System.out.print(parentlist[i]);
      System.out.print(", sibling = ");
      System.out.print(siblinglist[i]);
      System.out.print(", degree = ");
      System.out.print(degreelist[i]);
      System.out.print(", depth = ");
      System.out.print(depthlist[i]);
      System.out.print(", height = ");
      System.out.print(heightlist[i]);
      if(rootcheck[i]){
        System.out.println(", root");
      }else if(leafcheck[i]){
        System.out.println(", leaf");
      }else{
        System.out.println(", internal node");
      }
    }
  }
 
//nextChar を追加したよ！
  
// LinkedList<Integer>[] setsu = new LinkedList[n];
// for(int i = 0; i < n; i++){
//   setsu[i] = new LinkedList<Integer>();
// } 

 //ここからテンプレ
  static class InputReader {
      private InputStream stream;
      private byte[] buf = new byte[1024];
      private int curChar;
      private int numChars;
      private SpaceCharFilter filter;

      public InputReader(InputStream stream) {
          this.stream = stream;
      }
 
      public int next() {
          if (numChars == -1)
              throw new InputMismatchException();
          if (curChar >= numChars) {
              curChar = 0;
              try {
                  numChars = stream.read(buf);
              } catch (IOException e) {
                  throw new InputMismatchException();
              }
              if (numChars <= 0)
                  return -1;
          }
          return buf[curChar++];
      }
 
      public String nextStr() {
        int c = next();
        while(isSpaceChar(c)){c = next();}
        StringBuffer str = new StringBuffer();
        do{
          str.append((char)c);
          c = next();
        }while(!isSpaceChar(c));
        return str.toString();
      }

      public char nextChar() {
        int c = next();
        while(isSpaceChar(c)){c = next();}
        char ret;
        do{
          ret = (char)c;
          c = next();
        }while(!isSpaceChar(c));
        return ret;
      }

      public int nextInt() {
          int c = next();
          while (isSpaceChar(c))
              c = next();
          int sgn = 1;
          if (c == '-') {
              sgn = -1;
              c = next();
          }
          int res = 0;
          do {
              if (c < '0' || c > '9')
                  throw new InputMismatchException();
              res *= 10;
              res += c - '0';
              c = next();
          } while (!isSpaceChar(c));
          return res * sgn;
      }
 
      public boolean isSpaceChar(int c) {
          if (filter != null)
              return filter.isSpaceChar(c);
          return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
      }
 
      public interface SpaceCharFilter {
          public boolean isSpaceChar(int ch);
      }
  }
}