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 (Python):
import sys

N = int(sys.stdin.readline().rstrip())  

parents = [None] * N
sibling = [-1] * N
degree = [0] * N
depth = [None] * N
height = [None] * N
type_ = [None] * N

CHs = [[] for _ in range(N)]




def dfs(u, d, r=-1):

    
    tmp = "leaf"
    h = 0

    for v in CHs[u]:
        if v == r:
            continue  
        tmp = "internal node"
        depth[v] = depth[u] + 1
        h = max(dfs(v, d + 1, u) + 1, h)  

    
    if r == -1:
        tmp = "root"
    type_[u] = tmp

    height[u] = h

    return h


def main():
    for i in range(N):
        id, lf, rg = map(int, sys.stdin.readline().rstrip().split())

        if lf != -1:
            parents[lf] = id
            degree[id] = 1
            CHs[id].append(lf)
        if rg != -1:
            parents[rg] = id
            degree[id] = 1
            CHs[id].append(rg)
        if lf != -1 and rg != -1:
            sibling[lf] = rg
            sibling[rg] = lf
            degree[id] = 2

    
    u = 0
    while True:
        if parents[u] is None:
            root = u
            parents[u] = -1
            break
        u = parents[u]
    depth[root] = 0
    sibling[root] = -1

    

    dfs(root, 0)

    for i in range(N):
        print(f"node {i}: parent = {parents[i]},", end="")
        print(f" sibling = {sibling[i]},", end="")
        print(f" degree = {degree[i]},", end="")
        print(f" depth = {depth[i]},", end="")
        print(f" height = {height[i]}, {type_[i]}")


main()

Solution (Python):
from collections import deque
n = int(input())
nodes = []
siblings = [-1]*n
parents = [-1]*n
degrees = [0]*n
depths = [-1]*n
heights = [-1]*n
roles = ['leaf']*n
children = []

infs = []
for i in range(n):
    inf = list(map(int, input().split()))
    infs.append(inf)
infs.sort()

for i in range(n):
    inf = infs[i]
    node = inf[0]
    sibling = inf[1:]
    nodes.append(node)
    children.append(sibling)
    a, b = sibling[0], sibling[1]
    if a==-1 and b==-1:
        heights[i] = 0
    elif b == -1:
        parents[a] = i
        degrees[i] = 2-sibling.count(-1)
        roles[i] = 'internal node'
    elif a== -1:
        parents[b] = i
        degrees[i] = 2-sibling.count(-1)
        roles[i] = 'internal node'
    else:
        parents[a], parents[b] = i, i
        siblings[a], siblings[b] = b, a
        degrees[i] = 2-sibling.count(-1)
        roles[i] = 'internal node'
        
for i in range(n):
    if parents[i] == -1:
        roles[i] = 'root'
        depths[i] = 0

nums = deque([i for i in range(n)])
while len(nums) > 0:
    t = nums[0]
    nums.popleft()
    if depths[t]>=0 and degrees[t]>0:
        a, b = children[t][0], children[t][1]
        depths[a], depths[b] = depths[t]+1, depths[t]+1
    elif degrees[t]>0:
        nums.append(t)
    
            
nums = [i for i in range(n)]
i = 0
while len(nums) > 0:
    l = []
    for t in nums:
        if heights[t] == i:
            p = parents[t]
            if p == -1:
                continue
            heights[p] = i+1
            if p not in l:
              l.append(p)
        else:
            l.append(t)
    i += 1
    nums = l
        
for i in range(n):
    print('node', i, end='')
    print(': parent =', parents[i], end='')
    print(', sibling =', siblings[i], end='')
    print(', degree =', degrees[i], end='')
    print(', depth =', depths[i], end='')
    print(', height =', heights[i], end='')
    print(',', roles[i])

Solution (Python):
class Node:
    def __init__(self):
        self.parent = self.sibling = -1
        self.depth = self.height = 0
        self.left = self.right = -1
    def setDepth(self, depth):
        global t
        self.depth = depth
        if self.left >= 0:
            leftHeight = t[self.left].setDepth(depth + 1) + 1
        else:
            leftHeight = 0
        if self.right >= 0:
            rightHeight = t[self.right].setDepth(depth + 1) + 1
        else:
            rightHeight = 0
        self.height = max(leftHeight, rightHeight)
        return self.height

n = int(input())
t = [ Node() for i in range(n) ]
for i in range(n):
    no, left, right = map(int, input().split())
    t[no].left = left
    t[no].right = right
    if left >= 0:
        t[left].parent = no
        t[left].sibling = right
    if right >= 0:
        t[right].parent = no
        t[right].sibling = left

for i in range(n):
    if t[i].parent == -1:
        t[i].setDepth(0)

for i in range(n):
    print('node {0}: parent = {1}, sibling = {2}, '.\
        format(i, t[i].parent, t[i].sibling), end='')
    print('degree = {0}, '.\
        format((t[i].left >= 0)+(t[i].right >= 0)), end='')
    print('depth = {0}, height = {1}, '.\
        format(t[i].depth, t[i].height), end='')
    if t[i].parent < 0:
        print('root')
    elif t[i].height == 0:
        print('leaf')
    else:
        print('internal node')

Solution (Python):
import sys
sys.setrecursionlimit(10**7)

class Node:
    def __init__(self, parent, left, right):
        self.parent = parent
        self.left = left  
        self.right = right

def get_height(T, H, u):
    h1, h2 = 0, 0
    if T[u].right is not None:
        h1 = get_height(T, H, T[u].right) + 1
    if T[u].left is not None:
        h2 = get_height(T, H, T[u].left) + 1
    h = max(h1, h2)
    H[u] = h
    return h


def get_depth(T, D, u, d):
    D[u] = d
    if T[u].right is not None:
        get_depth(T, D, T[u].right, d+1) 
    if T[u].left is not None:
        get_depth(T, D, T[u].left, d+1) 

def ret_sibling(T, u):
    parent = T[u].parent
    if parent is None:
        return -1
    if T[parent].left != u:
        ret = T[parent].left
    else:
        ret = T[parent].right
    if ret is None:
        return -1
    return ret

def ret_degree(T, u):
    ret = 2
    if T[u].left is None:
        ret -= 1
    if T[u].right is None:
        ret -= 1
    return ret

n = int(input())
T = {k: Node(None, None, None) for k in range(n)}  
for _ in range(n):
    tmp = list(map(int, input().split()))
    if tmp[1] != -1:
        T[tmp[0]].left = tmp[1]  
        T[tmp[1]].parent = tmp[0]  
    if tmp[2] != -1:
        T[tmp[0]].right = tmp[2]  
        T[tmp[2]].parent = tmp[0]

for k, v in T.items():
    if v.parent is None:
        ROOT = k
        break

D = {}
get_depth(T, D, ROOT, 0)
H = {}
get_height(T, H, ROOT)


def print_for_a_node(u):
    if T[u].parent is None:
        parent = -1
    else:
        parent = T[u].parent

    sib = ret_sibling(T, u)
    deg = ret_degree(T, u)
    node_type = 'internal node' if deg != 0 else 'leaf'
    if parent == -1:
        node_type = 'root'

    print(f'node {u}: parent = {parent}, sibling = {sib}, degree = {deg}, depth = {D[u]}, height = {H[u]}, {node_type}')


for node in range(n):
    print_for_a_node(node)

Solution (Python):
class Node():
    def __init__(self):
        self.left = -1
        self.right = -1
        self.parent = -1
        self.sibling = -1
        self.degree = -1
        self.depth = -1
        self.height = -1
        self.type = None
        
n = int(input())
A = [list(map(int, input().split())) for _ in range(n)]

T = [Node() for i in range(25)]

for (idx, left, right) in A:
    T[idx].left = left
    T[idx].right = right
    T[idx].degree = ((left!=-1) + (right!=-1))
    if left != -1:
        T[left].parent = idx
    if right != -1:
        T[right].parent = idx
    if (left!=-1) & (right!=-1):
        T[left].sibling = right
        T[right].sibling = left
        
for idx in range(n):
    if T[idx].parent == -1:
        T[idx].type = 'root'
        root = idx
    elif T[idx].degree == 0:
        T[idx].type = 'leaf'
    else:
        T[idx].type = 'internal node'
        

def set_depth(u, d):
    T[u].depth = d
    if T[u].left != -1:
        set_depth(T[u].left, d+1)
    if T[u].right != -1:
        set_depth(T[u].right, d+1)
        
def set_height(u):
    hl = hr = 0
    if T[u].left != -1:
        hl = set_height(T[u].left) + 1
    if T[u].right != -1:
        hr = set_height(T[u].right) + 1
    h = max(hl, hr)
    T[u].height = h
    return h

set_depth(root, 0)
_ = set_height(root)

for idx in range(n):
    print('node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}'.format(idx, T[idx].parent, T[idx].sibling, T[idx].degree, T[idx].depth, T[idx].height, T[idx].type))

Solution (Python):
class Node:
    def __init__(self, node, left, right):
        self.node = node
        self.left = left
        self.right = right
        self.parent = -1
        self.sibling = -1
        self.degree = 0
        self.depth = 0
        self.height = 0
        self.type = ""

    def __str__(self):
        if self.parent == -1:
            self.type = "root"
        elif self.degree == 0:
            self.type = "leaf"
        else:
            self.type = "internal node"
        return "node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}".format(
            self.node, self.parent, self.sibling, self.degree, self.depth, self.height, self.type)


def dfs(tree, v, p, d, s):
    tree[v].parent = p
    tree[v].depth = d
    tree[v].sibling = s
    if tree[v].left == tree[v].right:
        tree[v].height = 0
        return 0
    maxheight = 0
    if tree[v].left != -1:
        tree[v].degree += 1
        maxheight = max(
            maxheight, dfs(
                tree, tree[v].left, v, d + 1, tree[v].right))
    if tree[v].right != -1:
        tree[v].degree += 1
        maxheight = max(
            maxheight, dfs(
                tree, tree[v].right, v, d + 1, tree[v].left))
    tree[v].height = maxheight + 1
    return tree[v].height


def resolve():
    n = int(input())
    tree = [None for i in range(n)]
    V = [list(map(int, input().split())) for _ in range(n)]
    hasChild = set()
    childrens = set()
    for a, left, right in V:
        tree[a] = Node(a, left, right)
        if left != right:
            hasChild.add(a)
            childrens.add(left)
            childrens.add(right)
    root = hasChild - childrens
    if len(root) > 0:
        dfs(tree, root.pop(), -1, 0, -1)
    print(*tree, sep="\n")


resolve()

Solution (Python):
n=int(input())
A=[list(map(int,input().split())) for i in range(n)]
B,H=[],[0]*n

def binary(A,B,node,parent,sibling,depth,type):
    if node!=-1:
        id,left,right=A[node]
        degree=len([i for i in (left,right) if i!=-1])
        if parent==-1:
            type="root"
        elif degree!=0:
            type="internal node"
        else:
            type="leaf"
        B.append([node,parent,sibling,degree,depth,type])
        binary(A,B,left,node,right,depth+1,type)
        binary(A,B,right,node,left,depth+1,type)

def get_height(H,u):
    h1,h2=0,0
    if A[u][2]!=-1:
        h1=get_height(H,A[u][2])+1
    if A[u][1]!=-1:
        h2=get_height(H,A[u][1])+1
    H[u]=max(h1,h2)
    return H[u]

top=[i for i in range(n)]
for i in range(n):
    for j in (A[i][1],A[i][2]):
        if j!=-1:
            top.remove(j)
top=top[0]
A=sorted(A)
binary(A,B,top,-1,-1,0,"root")
get_height(H,top)
B=sorted(B)

for j,i in enumerate(B):
    print("node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}".format(i[0],i[1],i[2],i[3],i[4],H[j],i[5]))

Solution (Python):
ID, LT, RT, PARENT, SIBLING, DEGREE, TYPE, DEPTH, HEIGHT = 1,2,3,4,5,6,7,8,9

def dfs(node, i):
  l,r = i,i
  if node[LT] != -1:
    l = dfs(nodes[node[LT]], i+1)
  if node[RT] != -1:
    r = dfs(nodes[node[RT]], i+1)
  return max(l, r)
  
def depth(node, i):
  if node[PARENT] == -1:
    return i
  return depth(nodes[node[PARENT]], i+1)

n = int(input())
nodes = [{} for _ in range(n)]

for node in nodes:
  id, lt, rt = list(map(int, input().split()))
  nodes[id][ID] = id
  nodes[id][LT] = lt
  nodes[id][RT] = rt
  nodes[id][SIBLING] = -1
  nodes[id][TYPE] = 'root'
  nodes[id][DEPTH] = 0
  nodes[id][PARENT] = -1
  nodes[id][HEIGHT] = 0
  nodes[id][DEGREE] = 0

for node in nodes:
  id = node[ID]
  deg = 0
  if node[LT] != -1:
    nodes[node[LT]][PARENT] = id
    if node[RT] == -1:
      nodes[node[LT]][SIBLING] = -1
    else:
      nodes[node[LT]][SIBLING] = nodes[node[RT]][ID]
    deg += 1
  if node[RT] != -1:
    nodes[node[RT]][PARENT] = id
    if node[LT] == -1:
      nodes[node[RT]][SIBLING] = -1
    else:
      nodes[node[RT]][SIBLING] = nodes[node[LT]][ID]
    deg += 1
  nodes[id][DEGREE] = deg


for node in nodes:
  nodes[node[ID]][HEIGHT] = dfs(node, 0)
  nodes[node[ID]][DEPTH] = depth(node, 0)
  if nodes[node[ID]][DEGREE] == 0 and nodes[node[ID]][PARENT] != -1:
    nodes[node[ID]][TYPE] = 'leaf'
  elif nodes[node[ID]][DEGREE] != 0 and nodes[node[ID]][PARENT] != -1:
    nodes[node[ID]][TYPE] = 'internal node'

for i in range(len(nodes)):
  node = nodes[i]
  print ('node {ID}: parent = {PARENT}, sibling = {SIBLING}, degree = {DEGREE}, depth = {DEPTH}, height = {HEIGHT}, {TYPE}'
    .format(ID=node[ID], PARENT=node[PARENT], SIBLING=node[SIBLING], DEGREE=node[DEGREE], DEPTH=node[DEPTH], HEIGHT=node[HEIGHT], TYPE=node[TYPE]))

Solution (Python):
import sys, collections
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    n = I()
    children = [[] for _ in range(n)]
    parent = [-1]*n
    sibling = [-1]*n
    for _ in range(n):
        ilr = LI()
        id = ilr[0]
        left = ilr[1]
        right = ilr[2]
        children[id] = (left, right)
        if left>=0:
            parent[left] = id
            sibling[left] = right
        if right>=0:
            parent[right] = id
            sibling[right] = left

    depth = [-1]*n
    root = parent.index(-1)
    depth[root] = 0
    que = collections.deque()
    que.append(root)
    while que:
        c = que.popleft()
        for i in children[c]:
            if i!=-1 and depth[i]==-1:
                que.append(i)
                depth[i] = depth[c] + 1

    height = [-1]*n
    def dfs(v):
        children_v = children[v]
        if children_v.count(-1)==2:
            height[v] = 0
        else:
            for i in children_v:
                if i!=-1:
                    height[v] = max(1 + dfs(i), height[v])
        return height[v]
    dfs(root)

    node_type = ['']*n
    for i in range(n):
        if parent[i]==-1:
            node_type[i] = 'root'
        elif children[i].count(-1)==2:
            node_type[i] = 'leaf'
        else:
            node_type[i] = 'internal node'

    for i in range(n):
        print('node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}'.format(i, parent[i], sibling[i], len(children[i])-children[i].count(-1), depth[i], height[i], node_type[i]))

if __name__ == '__main__':
    resolve()

Solution (Python):
n = int(input())

tree_info = {}
for i in range(n):
    row = input().split()
    childs = list(map(int, row[1:]))
    tree_info[int(row[0])] = [ int(child)  for child  in childs if child != -1]


Nodes = [None] * n

class Node:
    
    def __init__(self, id, parent, sibling, depth, child):
        self.id = id
        self.parent = parent
        self.depth = depth
        self.sibling = sibling
        self.height = 0
        if (parent == -1):
            self.tree_type = "root"
        elif(len(child) == 0):
            self.tree_type = "leaf"
        else:
            self.tree_type = "internal node"
            
        self.child = child
        
        if(self.tree_type != "leaf" and len(child) > 0):
            self.make_child()
    
    def make_child(self):
        if(len(self.child) == 2):
            Nodes[self.child[0]] = Node(self.child[0], self.id, self.child[1], self.depth + 1, tree_info[self.child[0]])
            Nodes[self.child[1]] = Node(self.child[1], self.id, self.child[0], self.depth + 1, tree_info[self.child[1]])
        elif(len(self.child) == 1):
            Nodes[self.child[0]] = Node(self.child[0], self.id, -1, self.depth + 1, tree_info[self.child[0]])
        
    def backward_height(self):
        if(self.tree_type == "root"):
            return
        if(self.tree_type == "leaf"):
            self.height = 0
        Nodes[self.parent].height = max(Nodes[self.sibling].height + 1, self.height + 1) 
        Nodes[self.parent].backward_height()
    
    def __str__(self):
        return "node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}".format(
            self.id, self.parent, self.sibling, len(self.child), self.depth, self.height, self.tree_type)


for i in range(n):
    if(Nodes[i] == None):
        Nodes[i] = Node(i, -1, -1, 0, tree_info[i])

for i in range(n):
    if(Nodes[i].tree_type == "leaf"):
        Nodes[i].backward_height()

for root in Nodes:
    print(root)

Solution (Python):
from collections import deque


def main():
    n = int(input())
    trees = [[] for _ in range(n)]
    sibs = [-1 for _ in range(n)]
    for i in range(n):
        tree = list(map(int, input().split()))
        
        if tree[1] != -1 and tree[2] != -1:
            trees[tree[0]] = tree[1:]
            sibs[tree[1]] = tree[2]
            sibs[tree[2]] = tree[1]
        elif tree[1] != -1:
            trees[tree[0]].append(tree[1])
            sibs[tree[1]] = -1
        elif tree[2] != -1:
            trees[tree[0]].append(tree[2])
            sibs[tree[2]] = -1
    

    
    candidates = set()
    have_parents = set()
    children = []
    for i in range(len(trees)):
        if len(trees[i]):
            candidates.add(i)
            children.extend(trees[i])
    children = set(children)
    have_parents = {c for c in candidates if c in children}

    if n == 1:
        root_node = 0
    else:
        root_node = list(candidates - have_parents)[0]

    depth = []
    q = deque()
    seen = [0]*n
    depth = [0]*n
    parents = [0]*n
    leaf_node = []
    heights = [0]*n

    q.append(root_node)
    seen[root_node] = 1
    parents[root_node] = -1

    
    while len(q):
        v = q.pop()
        d = depth[v] + 1
        for t in trees[v]:
            if seen[t]:
                continue
            seen[t] += 1
            parents[t] = v
            q.append(t)
            depth[t] = d
            if not len(trees[t]):
                leaf_node.append(t)

    for l in leaf_node:
        v = l
        height = 0
        while v != -1:
            heights[v] = max(heights[v], height)
            height += 1
            v = parents[v]

    for i in range(n):
        if not depth[i]:
            print(
                "node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, root"
                .format(i, parents[i], sibs[i], len(trees[i]), depth[i], heights[i]))
        elif depth and len(trees[i]):
             print(
                "node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, internal node"
                .format(i, parents[i], sibs[i], len(trees[i]), depth[i], heights[i]))
        else:
             print(
                "node {}: parent = {}, sibling = {}, degree = 0, depth = {}, height = {}, leaf"
                .format(i, parents[i], sibs[i], depth[i], heights[i]))

if __name__ == '__main__':
    main()

Solution (Python):

import sys
sys.setrecursionlimit(10 ** 7)


def resolve():
    class Node:
        def __init__(self, parent, left, right):
            self.parent = parent
            self.left = left
            self.right = right
            self.height = None

        def get_type(self):
            if self.parent == -1:
                return "root"
            elif self.left == -1 and self.right == -1:
                return "leaf"
            else:
                return "internal node"

        def get_depth(self):
            if self.parent == -1:
                return 0
            else:
                depth = 1
                t = Nodes[self.parent]
                while t.parent != -1:
                    t = Nodes[t.parent]
                    depth += 1
                return depth

        def get_height(self):
            h_left = 0
            h_right = 0
            if self.left != -1:
                h_left = Nodes[self.left].get_height() + 1
            if self.right != -1:
                h_right = Nodes[self.right].get_height() + 1
            self.height = max(h_left, h_right)
            return self.height

        def get_degree(self):
            degree = 0
            if self.left != -1:
                degree += 1
            if self.right != -1:
                degree += 1
            return degree

        def get_sigling(self):
            if self. parent == -1:
                return -1
            p = Nodes[self.parent]
            if Nodes[p.left] != self and Nodes[p.left] != -1:
                return p.left
            if Nodes[p.right] != self and Nodes[p.right] != -1:
                return p.right


    N = int(input())
    Nodes = [Node(-1, None, None) for _ in range(25)]

    for _ in range(N):
        id, left, right = map(int, input().split())
        Nodes[id].left = left
        Nodes[id].right = right
        if left != -1:
            Nodes[left].parent = id
        if right != -1:
            Nodes[right].parent = id

    for i in range(N):
        node = Nodes[i]
        print("node %d: " % i, end="")
        print("parent = %d, " % node.parent, end="")
        print("sibling = %d, " % node.get_sigling(), end="")
        print("degree = %d, " % node.get_degree(), end="")
        print("depth = %d, " % node.get_depth(), end="")
        print("height = %d, " % node.get_height(), end="")
        print(node.get_type())

if __name__ == '__main__':
    resolve()

Solution (Python):
class Node:
    def __init__(self):
        self.parent=-1
        self.left=-1
        self.right=-1

nodeList=[]
depthList=[]
heightList=[]

def readinput():
    global nodeList

    n=int(input())
    nodeList = [Node() for _ in range(n)]
    for _ in range(n):
        id,left,right = list(map(int,input().split()))
        nodeList[id].left=left
        if(left!=-1):
            nodeList[left].parent=id
        nodeList[id].right=right
        if(right!=-1):
            nodeList[right].parent=id
        
        
    return

def printNodeList():
    global nodeList
    print('\n[printNodeList]')
    for i,node in enumerate(nodeList):
        print(i, node.parent, node.left, node.right)
    print('')

def findRoot():
    global nodeList

    for i, node in enumerate(nodeList):
        if node.parent==-1:
            return i

def getDepth(p,d):
    global depthList
    global nodeList

    depthList[p]=d
    l=nodeList[p].left
    if(l!=-1):
        getDepth(l,d+1)
    r=nodeList[p].right
    if(r!=-1):
        getDepth(r,d+1)

def getHeight(p):
    global heightList
    global nodeList

    pnode=nodeList[p]
    if(pnode.left==-1 and pnode.right==-1):
        heightList[p]=0
    elif(pnode.left==-1):
        heightList[p]=getHeight(pnode.right)+1
    elif(pnode.right==-1):
        heightList[p]=getHeight(pnode.left)+1
    else:
        heightList[p]=max(getHeight(pnode.left),getHeight(pnode.right))+1
    return heightList[p]

def nodeType(id):
    global nodeList

    node=nodeList[id]
    if(node.parent==-1):
        return 'root'
    elif(node.left==-1 and node.right==-1):
        return 'leaf'
    else:
        return 'internal node'

def getSibling(id):
    global nodeList

    node=nodeList[id]
    if(node.parent==-1):
        return -1
    parent=nodeList[node.parent]
    if(parent.left==id):
        return parent.right
    else:
        return parent.left

def getChild(id):
    global nodeList

    node=nodeList[id]
    if (node.left==-1 and node.right==-1):
        return 0
    elif (node.left==-1 or node.right==-1):
        return 1
    else:
        return 2

def main():
    global nodeList
    global depthList
    global heightList
    depthList = [0]*len(nodeList)
    heightList = [0]*len(nodeList)

    root=findRoot()
    getDepth(root,0)
    getHeight(root)

    for i,node in enumerate(nodeList):
        print('node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}'\
            .format(i, node.parent, getSibling(i), getChild(i), depthList[i], heightList[i], nodeType(i) ))


    return

if __name__=='__main__':
    readinput()
    main()

Solution (Python):
from sys import stdin
input = stdin.readline

class Node:    
    def __init__(self, number):
        self.number  = number
        self.parent, self.sibling, self.degree, self.depth, self.height = -1,-1,0,0,0
        self.nodetype = 'internal node'
        self.child = []
    
    def output(self):
        print('node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %s, %s'%(self.number,self.parent,self.sibling,self.degree,self.depth,self.height,self.nodetype))

def check(node, depth):
    node.depth = depth
    if node.child != []:
        depth += 1
        for num in node.child:
            node.height = max(check(nodelist[num],depth) + 1, node.height)
    return node.height

n = int(input())
nodelist = [Node(i) for i in range(n)]
for _ in range(n):
    idx, left, right = map(int, input().split())
    if left == -1 and right == -1:
        nodelist[idx].nodetype = 'leaf'
    elif left != -1 and right != -1:
        nodelist[idx].child = [left,right]
        nodelist[idx].degree = 2
        nodelist[left].sibling, nodelist[right].sibling = right, left
        nodelist[left].parent, nodelist[right].parent = idx, idx
    elif left == -1 or right == -1:
        exist = max(left,right)
        nodelist[idx].child = [exist]
        nodelist[idx].degree = 1
        nodelist[exist].parent = idx

for node in nodelist:
    if node.parent == -1:
        node.nodetype = 'root'
        node.height = check(node, 0)
        break

for node in nodelist:
    node.output()

Solution (Python):
MAX = 10000
NIL = -1

class Node:
    def __init__(self):
        self.parent = NIL
        self.left = 0
        self.right = 0


D = [None for i in range(MAX)]

H = [None for i in range(MAX)]


def setDepth(u,d):
    if u == NIL: return
    D[u] = d
    setDepth(T[u].left, d + 1)
    setDepth(T[u].right, d + 1)


def setHeight(u):
    h1 = 0
    h2 = 0
    if T[u].left != NIL:
        h1 = setHeight(T[u].left)+1
    if T[u].right != NIL:
        h2 = setHeight(T[u].right)+1
    H[u] = max(h1,h2)
    return H[u]


def getSibling(u):
    if T[u].parent == NIL:
        return NIL
    if T[T[u].parent].left != u and T[T[u].parent].left != NIL:
        return T[T[u].parent].left
    if T[T[u].parent].right != u and T[T[u].parent].right != NIL:
        return T[T[u].parent].right
    return NIL

n = int(input())

T = []

for loop in range(n):
    node = Node()
    T.append(node)


def printNode(u):
    deg = 0
    if T[u].left != NIL:
        deg += 1
    if T[u].right != NIL:
        deg += 1
    print("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %d, "%(u, T[u].parent,  getSibling(u), deg, D[u], H[u]),end = "")

    if T[u].parent == NIL:
        print("root")
    elif T[u].left == NIL and T[u].right == NIL:
        print("leaf")
    else:
        print("internal node")

for i in range(n):
    v,l,r = map(int,input().split())
    T[v].left = l
    T[v].right = r
    if l != NIL:
        T[l].parent = v
    if r != NIL:
        T[r].parent = v


for i in range(n):
    if T[i].parent == NIL:
        root = i
        setDepth(root,0)
        setHeight(root)

for i in range(n):
    printNode(i)