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.



Sample input:
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:
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

Submission status: Runtime Error

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


def setHeight(u):
    global T, H
    hr = hl = 0
    if T[u].right != -1:
        hr = setHeight(T[u].right) + 1
    if T[u].left != -1:
        hl = setHeight(T[u].left) + 1
    H[u] = max(hr, hl)
    return H[u]


def setDepth(u, d):
    global T, D
    D[u] = d
    if T[u].right != -1: setDepth(T[u].right, d + 1)
    if T[u].left != -1: setDepth(T[u].left, d + 1)


def getSibling(u):
    global T
    if T[u].parent == -1: return -1

    if T[T[u].parent].right != u and T[T[u].parent].right != -1:
        return T[T[u].parent].right
    if T[T[u].parent].left != u and T[T[u].parent].left != -1:
        return T[T[u].parent].left

    return -1


def deg(u):
    global T
    dg = 0
    if T[u].right != -1: dg += 1
    if T[u].left != -1: dg += 1
    return dg


def label(u):
    if T[u].parent == -1:
        return "root"
    elif T[u].right == -1 and T[u].left == -1:
        return "leaf"
    else:
        return "internal node"


if __name__ == "__main__":
    n = int(input())
    T = []
    D = [-1] * n
    H = [-1] * n
    for i in range(n):
        tmp = list(map(int, input().split()))
        T.append(Node(tmp[0], tmp[1], tmp[2]))

    # parent?????£??\
    for i in range(n):
        if T[i].right != -1:
            T[T[i].right].parent = i
        if T[i].left != -1:
            T[T[i].left].parent = i

    setHeight(0)
    setDepth(0, 0)

    for i in range(n):
        print("node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}" \
              .format(T[i].id, T[i].parent, getSibling(i), deg(i), D[i], H[i], label(i)))

Submission status: Runtime Error

Solution code:
N = int(input())
M = []

type = ['root','internal node','leaf']

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

    def tostr(self):
        print('node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %d, %s' % (self.idx, self.parent, self.sibling, self.degree, self.depth, self.height, type[self.type])) 
tree = []


#'idx, parent, sibling, degree, depth, height, type'

for a in range(N):
    idx,left,right = map(int,input().strip().split(' '))
    tree.append(node(idx, -1, left,right))

for a in range(N):
    nd = tree[a]
    if nd.left != -1:
        tree[nd.left].parent = a
    if nd.right != -1:
        tree[nd.right].parent = a

    tree[nd.left].sibling = nd.right
    tree[nd.right].sibling = nd.left

    if nd.left == nd.right :
        nd.type=2
        nd.degree=0
    else:
        nd.type=1
        if nd.left == -1 or nd.right == -1:
            nd.degree = 1
        else:
            nd.degree = 2

root = ''
for a in range(N):
    if tree[a].parent == -1:
        root = a
        tree[root].type=0
    
def search(root,depth):
    root = tree[root]
    root.depth = depth
    if root.type ==  2:
        return
    else:
        search(root.right,depth+1)
        search(root.left,depth+1)

search(root,0)

def height(root,hei):
    root = tree[root]
    if root.type==2:
        root.height=0
        return hei
    else:
        return max(height(root.left,hei+1),height(root.right,hei+1))

for i in range(N):
    tree[i].height=height(i,0)




for a in range(N):
    tree[a].tostr()

Submission status: Runtime Error

Solution code:
N = int(input())
M = []

type = ['root','internal node','leaf']

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

    def tostr(self):
        print('node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %d, %s' % (self.idx, self.parent, self.sibling, self.degree, self.depth, self.height, type[self.type])) 
tree = []


#'idx, parent, sibling, degree, depth, height, type'

for a in range(N):
    idx,left,right = map(int,input().strip().split(' '))
    tree.append(node(idx, -1, left,right))

for a in range(N):
    nd = tree[a]
    if nd.left != -1:
        tree[nd.left].parent = a
    if nd.right != -1:
        tree[nd.right].parent = a

    tree[nd.left].sibling = nd.right
    tree[nd.right].sibling = nd.left

    if nd.left == nd.right :
        nd.type=2
        nd.degree=0
    else:
        nd.type=1
        if nd.left == -1 or nd.right == -1:
            nd.degree = 1
        else:
            nd.degree = 2

root = ''
for a in range(N):
    if tree[a].parent == -1:
        root = a
        tree[root].type=0
    
def search(root,depth):
    root = tree[root]
    root.depth = depth
    if root.type ==  2:
        return
    else:
        search(root.right,depth+1)
        search(root.left,depth+1)

search(root,0)

def height(root,hei):
    root = tree[root]
    if root.type==2:
        root.height=0
        return hei
    else:
        if root.left == -1:
            return height(root.right,hei+1)
        elif root.right == -1:
            return height(root.left,hei+1)
        else:
            return max(height(root.left,hei+1),height(root.right,hei+1))

for i in range(N):
    tree[i].height=height(i,0)




for a in range(N):
    tree[a].tostr()

Submission status: Runtime Error

Solution code:
N = int(input())
M = []

type = ['root','internal node','leaf']

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

    def tostr(self):
        print('node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %d, %s' % (self.idx, self.parent, self.sibling, self.degree, self.depth, self.height, type[self.type])) 
tree = [[] for x in range(N)]


#'idx, parent, sibling, degree, depth, height, type'

for a in range(N):
    idx,left,right = map(int,input().strip().split(' '))
    tree[idx] = node(idx, -1, left,right)

for a in range(N):
    nd = tree[a]
    if nd.left != -1:
        tree[nd.left].parent = a
    if nd.right != -1:
        tree[nd.right].parent = a
    if nd.left !=  -1:
        tree[nd.left].sibling = nd.right
    if nd.right !=  -1:
        tree[nd.right].sibling = nd.left

    if nd.left == nd.right :
        nd.type=2
        nd.degree=0
    else:
        nd.type=1
        if nd.left == -1 or nd.right == -1:
            nd.degree = 1
        else:
            nd.degree = 2

root = ''
for a in range(N):
    if tree[a].parent == -1:
        root = a
        tree[root].type=0
    
def search(root,depth):
    root = tree[root]
    root.depth = depth
    if root.type ==  2:
        return
    else:
        search(root.right,depth+1)
        search(root.left,depth+1)

search(root,0)

def height(root,hei):
    root = tree[root]
    if root.type==2:
        root.height=0
        return hei
    else:
        if root.left == -1:
            return height(root.right,hei+1)
        elif root.right == -1:
            return height(root.left,hei+1)
        else:
            return max(height(root.left,hei+1),height(root.right,hei+1))
for i in range(N):
    tree[i].height=height(i,0)

for a in range(N):
    tree[a].tostr()

Submission status: Runtime Error

Solution code:
# coding=utf-8

class Node:
    def __init__(self, ID, left, right):
        ### ?????¬?????± ###
        self.id = ID
        self.parent = -1 #??????
        self.sibling = -1 #??????
        self.degree = -1 #??????
        self.depth = 0 #??????
        self.height = 0 #??????
        self.type = None #??????

        ### ?£??¶??????± ###
        self.left_child = left
        self.right_child = right

def calc_depth(i):
    global node_list

    if node_list[i].depth != 0:
        return node_list[i].depth

    depth = 0
    if node_list[i].parent != -1:
        depth = calc_depth(node_list[i].parent) + 1

    node_list[i].depth = depth
    return depth


def calc_height(i):
    global node_list

    if node_list[i].height != 0:
        return node_list[i].height

    h1 = h2 = 0
    if node_list[i].right_child != -1:
        h1 = calc_height(node_list[i].right_child) + 1
    if node_list[i].left_child != -1:
        h2 = calc_height(node_list[i].left_child) + 1

    height = max(h1, h2)
    node_list[i].height = height
    return height

n = int(input())
data = [tuple(map(int, input().split())) for x in range(n)]
node_list = []

for data_row in data:
    ID, left, right = data_row
    node = Node(ID, left, right)
    node_list.append(node)

for i, node in enumerate(node_list):
    left = node.left_child
    right = node.right_child
    degree = 0

    ### ???????????± ###
    if left != -1:
        degree += 1
        node_list[left].parent = i
        node_list[left].sibling = right

    if right != -1:
        degree += 1
        node_list[right].parent = i
        node_list[right].sibling = left

    ### ??????????????± ###
    node.degree = degree

for i in range(n):
    calc_depth(i)
    calc_height(i)

    node = node_list[i]

    if node.depth == 0:
        node.type = 'root'
    elif node.height == 0:
        node.type = 'leaf'
    else:
        node.type = 'internal node'

    print("node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}".format(node.id, node.parent, node.sibling, node.degree, node.depth, node.height, node.type))

Submission status: Runtime Error

Solution code:
class Node():
    def __init__(self, id, left, right, parent=-1, sibling=-1, degree=-1, depth=-1, height=-1, type=None):
        self.id = id
        self.left = left
        self.right = right
        self.parent = parent
        self.sibling = sibling
        self.degree = degree
        self.depth = depth
        self.height = height
        self.type = type
    def show(self):
        print 'node %d: parent = %d, sibling = %d, degree = %d, depth = %d, height = %d, %s' % (self.id, self.parent, self.sibling, self.degree, self.depth, self.height, self.type)

def hasChild(x):
    if x == -1:
        return 0
    else:
        return 1

def calcDepth(node):
    if node.parent == -1:
        return 0
    return 1 + calcDepth(a[node.parent])

def calcHeight(node):
    if node.degree == 0:
        return 0
    return 1 + max(calcHeight(a[node.left]), calcHeight(a[node.right]))

n = int(raw_input())
a = []
for i in range(n):
    tmp = map(int, raw_input().split())
    if tmp[1] == -1 and tmp[2] == -1:
        a.append(Node(tmp[0], tmp[1], tmp[2], degree=0, type='leaf'))
    else:
        a.append(Node(tmp[0], tmp[1], tmp[2], degree=hasChild(tmp[1])+hasChild(tmp[2])))

for i in range(n):
    if a[i].right != -1:
        a[a[i].right].parent = a[i].id
    if a[i].left != -1:
        a[a[i].left].parent = a[i].id

for i in range(n):
    a[i].depth = calcDepth(a[i])
    a[i].height = calcHeight(a[i])
        
for i in range(n):
    if a[i].parent != -1 and a[i].degree != 0:
        a[i].type = 'internal node'
    elif a[i].parent == -1 and a[i].degree > 0:
        a[i].type = 'root'
    for j in range(n):
        if i == j:
            continue
        if a[i].depth == a[j].depth and a[i].parent == a[j].parent:
            a[i].sibling = j
        
for el in a:
    el.show()

Submission status: Runtime Error

Solution code:
from collections import defaultdict

def dfs(here, p, d, conn, parent, depth, height):
    parent[here] = p
    depth[here] = d
    l_d, r_d = 0, 0
    for c in conn[here]:
        if c[0] >= 0:
            l_d = dfs(c[0], here, d + 1, conn, parent, depth, height)
        if c[1] >= 0
            r_d = dfs(c[1], here, d + 1, conn, parent, depth, height)
    if len(conn[here]):
        height[here] = max(max(l_d, r_d) - d, 0)
    return max(l_d, r_d, d)

v_num = int(input())
conn = defaultdict(list)
in_v = [0 for n in range(v_num + 1)]
for _ in range(v_num):
    v, left, right = (int(n) for n in input().split(" "))
    conn[v].append([left, right])
    in_v[left] += 1
    in_v[right] += 1
    degree[v] += int(left >= 0) + int(right >= 0)
for i, n in enumerate(in_v):
    if not n:
        root = i
        break
parent = [-1 for n in range(v_num)]
depth = [0 for n in range(v_num)]
height = [0 for n in range(v_num)]
dfs(root, -1, 0, conn, parent, depth, height)
sibling = [-1 for n in range(v_num)]
degree = [0 for n in range(v_num)]
for i in range(v_num):
    for j in range(v_num):
        if i == j:
            continue
        elif parent[i] == parent[j]:
            sibling[i] = j
            break
v_type = ["" for n in range(v_num)]
for i in range(v_num):
    if i == root:
        v_type[i] = "root"
    elif len(conn[i]) != 0:
        v_type[i] = "internal node"
    else:
        v_type[i] = "leaf"
for i in range(v_num):
    print("node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}".format(i, parent[i], sibling[i], degree[i], depth[i], height[i], v_type[i]))

Submission status: Runtime Error

Solution code:
from collections import defaultdict

def dfs(here, p, d, conn, parent, depth, height):
    parent[here] = p
    depth[here] = d
    l_d, r_d = 0, 0
    for c in conn[here]:
        if c[0] >= 0:
            l_d = dfs(c[0], here, d + 1, conn, parent, depth, height)
        if c[1] >= 0:
            r_d = dfs(c[1], here, d + 1, conn, parent, depth, height)
    if len(conn[here]):
        height[here] = max(max(l_d, r_d) - d, 0)
    return max(l_d, r_d, d)

v_num = int(input())
conn = defaultdict(list)
in_v = [0 for n in range(v_num + 1)]
for _ in range(v_num):
    v, left, right = (int(n) for n in input().split(" "))
    conn[v].append([left, right])
    in_v[left] += 1
    in_v[right] += 1
    degree[v] += int(left >= 0) + int(right >= 0)
for i, n in enumerate(in_v):
    if not n:
        root = i
        break
parent = [-1 for n in range(v_num)]
depth = [0 for n in range(v_num)]
height = [0 for n in range(v_num)]
dfs(root, -1, 0, conn, parent, depth, height)
sibling = [-1 for n in range(v_num)]
degree = [0 for n in range(v_num)]
for i in range(v_num):
    for j in range(v_num):
        if i == j:
            continue
        elif parent[i] == parent[j]:
            sibling[i] = j
            break
v_type = ["" for n in range(v_num)]
for i in range(v_num):
    if i == root:
        v_type[i] = "root"
    elif len(conn[i]) != 0:
        v_type[i] = "internal node"
    else:
        v_type[i] = "leaf"
for i in range(v_num):
    print("node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}".format(i, parent[i], sibling[i], degree[i], depth[i], height[i], v_type[i]))

Submission status: Runtime Error

Solution code:
# -*- coding:utf-8 -*-
import sys


def bin_tree(lst, n):
    """
        tree[0]: node
        tree[1]: parent
        tree[2]: sibling
        tree[3]: degree
        tree[4]: depth
        tree[5]: height
    """
    info = [[-1, -1, -1, -1, -1, -1] for n in range(0, n)]
    degree = 0
    for node, left, right in lst:
        if left > 0:
            info[left][1] = node
            info[left][2] = right
            degree += 1

        if right > 0:
            info[right][1] = node
            info[right][2] = left
            degree += 1

        info[node][0] = node
        info[node][3] = degree

        degree = 0

    root = -1
    for node in info:
        root = node[0]
        if node[1] == -1:
            break

    get_depth(lst, root, info)
    get_height(lst, root, info)
    print_info(info)


def get_height(tree, root, info):
    def _get_height(node, depth):
        if node == -1:
            return 0

        left_height = _get_height(tree[node][1], depth + 1)
        right_height = _get_height(tree[node][2], depth + 1)

        if left_height > right_height:
            info[node][5] = left_height
        else:
            info[node][5] = right_height

        return info[node][5] + 1

    _get_height(root, 0)


def get_depth(tree, root, info):
    def _get_depth(node, depth):
        if node == -1:
            return

        info[node][4] = depth
        _get_depth(tree[node][1], depth + 1)
        _get_depth(tree[node][2], depth + 1)

    _get_depth(root, 0)


def print_info(info):
    for node, parent, sibling, degree, depth, height in info:
        if parent == -1:
            _type = "root"
        elif degree != 0:
            _type = "internal node"
        else:
            _type = "leaf"

        print("node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, height = {5}, {6}".format(node, parent, sibling, degree, depth, height, _type))


if __name__ == "__main__":
    n = int(input())
    lst = [[int(n) for n in val.split()] for val in sys.stdin.readlines()]
    bin_tree(lst, n)

Submission status: Runtime Error

Solution code:
import sys
readline = sys.stdin.readline
class Tree:
    __slots__ = ['id', 'p', 'sibling', 'degree', 'depth', 'height', 'type', 'c']
    def __init__(self, id):
        self.id = id
        self.p = -1
        self.sibling = -1
        self.degree = 0
        self.depth = 0
        self.height = 0
        self.type = "leaf"
        self.c = []
    def __str__(self):
        return f"node {self.id}: parent = {self.p}, sibling = {self.sibling}, degree = {self.degree}, depth = {self.depth}, height = {self.height}, {self.type}"
def solve():
    n = int(input())
    tree = tuple(Tree(i) for i in range(n))
    for _ in range(n):
        id, *c = map(int, readline().replace("-1", "").split())
        if c != []:
            tree[id].type = "internal node"
            tree[id].c = c
            for j in c:
                tree[j].p = id
            len_c = len(c)
            tree[id].degree = len_c
            if len_c == 2:
                tree[c[0]].sibling = c[1]
                tree[c[1]].sibling = c[0]
    rootget = lambda x: rootget(x) if tree[x].p != -1 else x
    root = rootget(0)
    tree[root].type = "root"
    def depth_height_check(id, d):
        d += 1
        height = 0
        for i in tree[id].c:
            tree[i].depth = d
            if tree[i].type != "leaf":
                tree[i].height = depth_height_check(i, d) + 1
                height = max(height, tree[i].height)
        return height
    tree[root].height = depth_height_check(root, 0) + 1 if n > 1 else 0
    print("\n".join(map(str, tree)))
solve()

Submission status: Runtime Error

Solution code:
def q(a,k):
 d[a]=k
 for b in t[a]:
  if'-1'!=b:q(b,k+1)
def n(a,k):
 h[a]=max(h[a],k)if a in h else k
 if'-1'!=p[a]:n(p[a],k+1)
t,p,s,d,h={},{},{},{},{}
for _ in[0]*int(input()):
 x,y,z=input().split()
 t[x]=set([y,z])-{-1}
 p[y]=p[z]=x
 s[y],s[z]=z,y
r=(set(t)-set(p)).pop()
p[r]=s[r]='-1'
q(r,0)
for i in t:
 if not t[i]:n(i,0)
for i in map(str,sorted(map(int,t))):
 print(f"node {i}: parent = {p[i]}, sibling = {s[i]}, degree = {len(t[i])}, depth = {d[i]}, height = {h[i]}, {'root'if d[i]==0else'internal node'if t[i]else'leaf'}")

Submission status: Runtime Error

Solution code:
def q(a,k):
 d[a]=k
 for b in t[a]:
  if'-1'!=b:q(b,k+1)
def n(a,k):
 h[a]=max(h[a],k)if a in h else k
 if'-1'!=p[a]:n(p[a],k+1)
t,p,s,d,h={},{},{},{},{}
n=int(input())
for _ in[0]*n:
 x,y,z=input().split()
 t[x]=set([y,z])-{'-1'}
 p[y]=p[z]=x
 s[y],s[z]=z,y
r=(set(t)-set(p)).pop()
p[r]=s[r]='-1'
q(r,0)
for i in t:
 if not t[i]:n(i,0)
for i in map(str,range(n)):
 print(f"node {i}: parent = {p[i]}, sibling = {s[i]}, degree = {len(t[i])}, depth = {d[i]}, height = {h[i]}, {'root'if d[i]==0else'internal node'if t[i]else'leaf'}")

Submission status: Runtime Error

Solution code:
def q(a,k):
 d[a]=k
 for b in t[a]:
  if'-1'!=b:q(b,k+1)
def n(a,k):
 h[a]=max(h[a],k)if a in h else k
 if'-1'!=p[a]:n(p[a],k+1)
t,p,s,d,h={},{},{},{},{}
N=int(input())
for _ in[0]*n:
 x,y,z=input().split()
 t[x]=set([y,z])-{'-1'}
 p[y]=p[z]=x
 s[y],s[z]=z,y
r=(set(t)-set(p)).pop()
p[r]=s[r]='-1'
q(r,0)
for i in t:
 if not t[i]:n(i,0)
for i in map(str,range(N)):
 print(f"node {i}: parent = {p[i]}, sibling = {s[i]}, degree = {len(t[i])}, depth = {d[i]}, height = {h[i]}, {'root'if d[i]==0else'internal node'if t[i]else'leaf'}")

Submission status: Runtime Error

Solution code:
N=int(input())
t=[0]*N
r=set(range(N))
for _ in[0]*N:
 x,y,z=list(map(int,input().split()))
 t[x]=[y,z]+[0]*2+[(l!=-1)+(r!=-1)]+[0]*2
 r-={y,z}
def f(i,p,s,d):
 if i<0:return-1
 l,r=t[i][:2]
 t[i][2:]=[p,s,t[i][4],d,max(f(l,i,r,d+1),f(r,i,l,d+1))+1]
 return t[i][-1]
f(r.pop(),-1,-1,0)
for i in range(N):
 print('node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}'.format(i,*t[i][2:],'root'if not t[i][5]else'internal node'if t[i][4]else'leaf'))

Submission status: Runtime Error

Solution code:
N=int(input())
t=[0]*N
r=set(range(N))
for _ in[0]*N:
 x,y,z=list(map(int,input().split()))
 t[x]=[y,z,0,0,(y!=-1)+(z!=-1),0,0]
 r-={y,z}
def f(i,p,s,d):
 if i<0:return-1
 t[i][2:]=[p,s,t[i][4],d,max(f(l,i,r,d+1),f(r,i,l,d+1))+1]
 return t[i][-1]
f(r.pop(),-1,-1,0)
for i in range(N):
 print('node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}'.format(i,*t[i][2:],'root'if not t[i][5]else'internal node'if t[i][4]else'leaf'))

Submission status: Runtime Error

Solution code:
N=int(input())
t=[0]*N
r=set(range(N))
for _ in[0]*N:
 e=list(map(int,input().split()))
 t[e[0]]=e[1:]+[0]*5
 r-={e[1:]}
def f(i,p,s,d):
 if i<0:return-1
 l,r=t[i][:2]
 t[i][2:]=[p,s,(l!=-1)+(r!=-1),d,max(f(l,i,r,d+1),f(r,i,l,d+1))+1]
 return t[i][-1]
f(r.pop(),-1,-1,0)
for i in range(N):
 print('node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}'.format(i,*t[i][2:],'root'if not t[i][5]else'internal node'if t[i][4]else'leaf'))

Submission status: Runtime Error

Solution code:
N=int(input())
t=[0]*N
r=set(range(N))
for _ in[0]*N:
 x,y,z=map(int,input().split())
 t[x]=[y,z]+[0]*5
 r-={y,z}
def f(i,p,s,d):
 if i<0:return-1
 l,r=t[i][:2]
 t[i][2:5]=[p,s,(l!=-1)+(r!=-1),d]
 return t[i][6]=max(f(l,i,r,d+1),f(r,i,l,d+1))+1
f(r.pop(),-1,-1,0)
for i in range(N):
 print('node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}'.format(i,*t[i][2:],'root'if not t[i][5]else'internal node'if t[i][4]else'leaf'))

Submission status: Runtime Error

Solution code:
NIL = -1

class Node:
    parent = NIL
    left = NIL
    right = NIL

n = int(input())
t = [Node() for i in range(n)]

for i in range(n):
    a = list(map(int, input().split()))
    if a[1] != -1:
        t[i].left = a[1]
        t[a[1]].parent = a[i]
    if a[2] != -1:
        t[i].right = a[2]
        t[a[2]].parent = a[i]

def getdepth(u):
    d = 0
    while t[u].parent != NIL:
        u = t[u].parent
        d += 1
    return d

d = []
for i in range(i):
    b = getdepth(i)
    d.append(b)
    


for i in range(n):
    c = 0
    if t[i].left != NIL:
        c += 1
    if t[i].right != NIL:
        c += 1
    
    if t[i].parent == NIL:
        cond = "root"
    elif t[i].left == NIL and t[i].right == NIL:
        cond = "leaf"
    else:
        cond = "internal node"
    
    print("node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}"
          .format(i, t[i].parent, d.count(d[i]), c, d[i], max(d)-d[i], cond))

Submission status: Runtime Error

Solution code:
n = int(input())
root = set(range(n))
binary_tree = []
out = [0 for i in range(n)]
for i in range(n):
    l = list(map(int, input().split()))
    binary_tree.append(l)
    s = set(l[1:3])
    root -= s

def binary(id, parent, depth, sibling, height):
    output = {}
    hline = []
    hline.append(height)
    output['node'] = id
    output['parent'] = parent
    output['sibling'] = sibling
    output['depth'] = depth
    left = binary_tree[id][1]
    right = binary_tree[id][2]
    degree = 0
    if(parent == -1):
        output['type'] = 'root'
    elif(left == -1 and right == -1):
        output['type'] = 'leaf'
    else:
        output['type'] = 'internal node'
    if(left != -1):
        degree += 1
        hline.append(binary(left, id, depth+1, right, height))
    if(right != -1):
        degree += 1
        hline.append(binary(right, id, depth+1, left, height))
    max_height = max(hline)
    output['degree'] = degree
    output['height'] = max_height
    out[output['node']] = output
    return max_height + 1

binary(list(root)[0], -1, 0, -1, 0)
for line in out:
    print('node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}'.format(line['node'], line['parent'], line['sibling'], line['degree'], line['depth'], line['height'], line['type']))