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: Wrong Answer

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

class Node(object):
    def __init__(self, N):
        self.parent = [-1 for i in range(N)]
        self.left = [-1 for i in range(N)]
        self.right = [-1 for i in range(N)]
        self.depth = [-1 for i in range(N)]
        self.sibling = [-1 for i in range(N)]
        self.hight = [0 for i in range(N)]
        self.degree = [0 for i in range(N)]

    def set_depth(self, u):
        d, U = 0, u
        while T.parent[u] is not -1:
            u = T.parent[u]
            d += 1
        self.depth[U] = d

    def set_hight(self, u):
        h1 = h2 = 0
        if self.left[u] is not -1:
            h1 = self.set_hight(self.left[u]) + 1
        if self.right[u] is not -1:
            h2 = self.set_hight(self.right[u]) + 1
        
        self.hight[u] = max(h1, h2)
        return max(h1, h2)
            

N = int(input())

# create node
T = Node(N)

# set data
for j in range(N):
    A = [int(i) for i in input().split()]

    # left???right????±????
    T.left[A[0]] = A[1]
    T.right[A[0]] = A[2]
    
    # parent???degree????±????
    for u in A[1:]:
        if u is not -1:
            T.parent[u] = A[0]
            T.degree[A[0]] += 1
            
    # sibling????±????
    if (A[1] is not -1) and (A[2] is not -1):
        T.sibling[A[1]], T.sibling[A[2]] = A[2], A[1]

# set depth
for i in range(N):
    T.set_depth(i)

# set hight
T.set_hight(T.parent.index(-1))

# ??????
for i in range(N):
    # ?????????????¨?????????????
    if T.parent[i] is -1:
        node = "root"
    elif T.left[i] is -1:
        node = "leaf"
    else:
        node = "internal node"

    print("node {0}: parent = {1}, sibling = {2}, degree = {3}, depth = {4}, hight = {5}, {6}".format(i, T.parent[i], T.sibling[i], T.degree[i], T.depth[i], T.hight[i], node))

Submission status: Wrong Answer

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

class Node(object):
    def __init__(self, N):
        self.parent = [-1 for i in range(N)]
        self.left = [-1 for i in range(N)]
        self.right = [-1 for i in range(N)]
        self.depth = [-1 for i in range(N)]
        self.sibling = [-1 for i in range(N)]
        self.height = [0 for i in range(N)]
        self.degree = [0 for i in range(N)]

    def set_depth(self, u):
        d, U = 0, u
        while T.parent[u] is not -1:
            u = T.parent[u]
            d += 1
        self.depth[U] = d

    def set_height(self, u):
        h1 = h2 = 0
        if self.left[u] is not -1:
            h1 = self.set_height(self.left[u]) + 1
        if self.right[u] is not -1:
            h2 = self.set_height(self.right[u]) + 1
        
        self.height[u] = max(h1, h2)
        return max(h1, h2)
            

N = int(input())

# create node
T = Node(N)

# set data
for j in range(N):
    A = [int(i) for i in input().split()]

    # left???right????±????
    T.left[A[0]] = A[1]
    T.right[A[0]] = A[2]
    
    # parent???degree????±????
    for u in A[1:]:
        if u is not -1:
            T.parent[u] = A[0]
            T.degree[A[0]] += 1
            
    # sibling????±????
    if (A[1] is not -1) and (A[2] is not -1):
        T.sibling[A[1]], T.sibling[A[2]] = A[2], A[1]

# set depth
for i in range(N):
    T.set_depth(i)

# set height
T.set_height(T.parent.index(-1))

# ??????
for i in range(N):
    # ?????????????¨?????????????
    if T.parent[i] is -1:
        node = "root"
    elif T.left[i] is -1:
        node = "leaf"
    else:
        node = "internal node"

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

Submission status: Wrong Answer

Solution code:
#coding:UTF-8
def depth(leaf,A,d):
    for i in range(len(A)):
        if A[i][1]==leaf or A[i][2]==leaf:
            return depth(A[i][0],A,d+1)
    return d

def height(n,A,h):
    if n[1]!=-1 and n[2]!=-1:
        return max(height(A[n[1]],A,h+1),height(A[n[2]],A,h+1))
    return h

def BT(A,n):
    for i in range(n):
        p=0
        dep=0
        deg=0
        sib=0
        hei=0
        t=""
        if A[i][1]!=-1 and A[i][2]!=-1:
            deg=2
        elif A[i][1]!=-1 or A[i][2]!=-1:
            deg=1
        else:
            deg=0
        if A[i][1]==-1 and A[i][2]==-1:
            t="leaf"
            deg=0
            dep=depth(A[i][0],A,dep)
            hei=height(A[i],A,hei)
            for j in range(len(A)):
                if A[i][0]==A[j][1]:
                    sib=A[j][2]
                    break
                elif A[i][0]==A[j][2]:
                    sib=A[j][1]
                    break
            for j in range(len(A)):
                if A[j][2]==A[i][0] or A[j][1]==A[i][0]:
                    p=A[j][0]
                    break
        else:
            hei=height(A[i],A,hei)
            for j in range(len(A)):
                if A[i][0]==A[j][1]:
                    sib=A[j][2]
                    break
                elif A[i][0]==A[j][2]:
                    sib=A[j][1]
                    break
            dep=depth(A[i][0],A,dep)
            if dep==0:
                sib=-1
                p=-1
                t="root"
            else:
                for j in range(len(A)):
                    if A[j][2]==A[i][0] or A[j][1]==A[i][0]:
                        p=A[j][0]
                        t="internal node"
                        break
        
        print("node "+str(A[i][0])+": parent = "+str(p)+", sibling = "+str(sib)+", degree = "+str(deg)+", depth = "+str(dep)+", height = "+str(hei)+", "+t)

if __name__=="__main__":
    n=int(input())
    A=[]
    for i in range(n):
        A.append(list(map(int,input().split(" "))))
    for i in range(n):
        minj=i
        for j in range(i,n):
            if A[j][0]<A[minj][0]:
                minj=j
        A[i],A[minj]=A[minj],A[i]
    BT(A,n)

Submission status: Wrong Answer

Solution code:
#coding:UTF-8
def depth(leaf,A,d):
    for i in range(len(A)):
        if A[i][1]==leaf or A[i][2]==leaf:
            return depth(A[i][0],A,d+1)
    return d

def height(n,A,h):
    if n[1]!=-1 and n[2]!=-1:
        return max(height(A[n[1]],A,h+1),height(A[n[2]],A,h+1))
    return h

def BT(A,n):
    for i in range(n):
        p=0
        dep=0
        deg=0
        sib=0
        hei=0
        t=""
        if A[i][1]!=-1 and A[i][2]!=-1:
            deg=2
        elif A[i][1]!=-1 or A[i][2]!=-1:
            deg=1
        else:
            deg=0
        if A[i][1]==-1 and A[i][2]==-1 and n>1:
            t="leaf"
            deg=0
            dep=depth(A[i][0],A,dep)
            hei=height(A[i],A,hei)
            for j in range(len(A)):
                if A[i][0]==A[j][1]:
                    sib=A[j][2]
                    break
                elif A[i][0]==A[j][2]:
                    sib=A[j][1]
                    break
            for j in range(len(A)):
                if A[j][2]==A[i][0] or A[j][1]==A[i][0]:
                    p=A[j][0]
                    break
        else:
            hei=height(A[i],A,hei)
            for j in range(len(A)):
                if A[i][0]==A[j][1]:
                    sib=A[j][2]
                    break
                elif A[i][0]==A[j][2]:
                    sib=A[j][1]
                    break
            dep=depth(A[i][0],A,dep)
            if dep==0:
                sib=-1
                p=-1
                t="root"
            else:
                for j in range(len(A)):
                    if A[j][2]==A[i][0] or A[j][1]==A[i][0]:
                        p=A[j][0]
                        t="internal node"
                        break
        
        print("node "+str(A[i][0])+": parent = "+str(p)+", sibling = "+str(sib)+", degree = "+str(deg)+", depth = "+str(dep)+", height = "+str(hei)+", "+t)

if __name__=="__main__":
    n=int(input())
    A=[]
    for i in range(n):
        A.append(list(map(int,input().split(" "))))
    for i in range(n):
        minj=i
        for j in range(i,n):
            if A[j][0]<A[minj][0]:
                minj=j
        A[i],A[minj]=A[minj],A[i]
    BT(A,n)

Submission status: Wrong Answer

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_7_B&lang=jp
def cal_depth(binary_tree, target_index, depth, parent, sibling):
    if target_index == -1:
        return 0
    binary_tree[target_index]["depth"] = depth
    binary_tree[target_index]["parent"] = parent
    binary_tree[target_index]["sibling"] = sibling
    height_left = cal_depth(binary_tree, binary_tree[target_index]["left"], depth + 1, target_index, binary_tree[target_index]["right"])
    height_right = cal_depth(binary_tree, binary_tree[target_index]["right"], depth + 1, target_index, binary_tree[target_index]["left"])
    height = max(height_left, height_right)
    binary_tree[target_index]["height"] = height
    return height + 1

def solve(node_data, node_num):
    binary_tree = [{"left":-1, "right":-1, "depth":0, "parent":-1, "sibling":-1, "height":0, "degree":0} for a in range(node_num)]
    root_index = sum([i for i in range(node_num)])
    for node in node_data:
        binary_tree[node[0]]["left"] =  node[1]
        binary_tree[node[0]]["right"] =  node[2]

        if not node[1] == -1:
            root_index -= node[1]
            binary_tree[node[0]]["degree"] += 1
        if not node[2] == -1:
            root_index -= node[2]
            binary_tree[node[0]]["degree"] += 1
            
    cal_depth(binary_tree, root_index, 0, -1,-1)
    return binary_tree

def main():
    node_num = int(input())
    node_data = [[int(a) for a in input().split()] for i in range(node_num)]
    binary_tree = solve(node_data, node_num)

    for i, node in enumerate(binary_tree):
        node_type = "root"
        if node["left"] == -1 and node["right"] == -1:
            node_type = "leaf"
        elif not node["parent"] == -1:
            node_type = "internal node"
        print("node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}".format(i, node["parent"], node["sibling"], node["degree"], node["depth"], node["height"], node_type))
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_7_B&lang=jp
def cal_depth(binary_tree, target_index, depth, parent, sibling):
    if target_index == -1:
        return 0
    binary_tree[target_index]["depth"] = depth
    binary_tree[target_index]["parent"] = parent
    binary_tree[target_index]["sibling"] = sibling
    height_left = cal_depth(binary_tree, binary_tree[target_index]["left"], depth + 1, target_index, binary_tree[target_index]["right"])
    height_right = cal_depth(binary_tree, binary_tree[target_index]["right"], depth + 1, target_index, binary_tree[target_index]["left"])
    height = max(height_left, height_right)
    binary_tree[target_index]["height"] = height
    return height + 1

def solve(node_data, node_num):
    binary_tree = [{"left":-1, "right":-1, "depth":0, "parent":-1, "sibling":-1, "height":0, "degree":0} for a in range(node_num)]
    root_index = sum([i for i in range(node_num)])
    for node in node_data:
        binary_tree[node[0]]["left"] =  node[1]
        binary_tree[node[0]]["right"] =  node[2]

        if not node[1] == -1:
            root_index -= node[1]
            binary_tree[node[0]]["degree"] += 1
        if not node[2] == -1:
            root_index -= node[2]
            binary_tree[node[0]]["degree"] += 1
            
    cal_depth(binary_tree, root_index, 0, -1,-1)
    return binary_tree

def main():
    node_num = int(input())
    node_data = [[int(a) for a in input().split()] for i in range(node_num)]
    binary_tree = solve(node_data, node_num)

    for i, node in enumerate(binary_tree):
        node_type = "root"
        if node["left"] == -1 and node["right"] == -1 and not i == 0:
            node_type = "leaf"
        elif not node["parent"] == -1:
            node_type = "internal node"
        print("node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}".format(i, node["parent"], node["sibling"], node["degree"], node["depth"], node["height"], node_type))
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import sys


class Node(object):
    def __init__(self):
        # init node as a root node.
        self.parent = -1
        self.type = 'leaf'
        self.depth = 0
        self.children = []
        self.degree = 0
        self.sibling = -1
        self.height = 0


def dfs(node_idx, node_list, depth=0):
    node_list[node_idx].depth = depth
    for child in node_list[node_idx].children:
        dfs(child, node_list, depth=depth + 1)
    return None


def get_height(v, node_list):
    h1, h2 = 0, 0
    if not v.children:
        return 0

    if v.children[0] != -1:
        h1 = get_height(node_list[v.children[0]], node_list=node_list) + 1

    if v.children[1] != -1:
        h2 = get_height(node_list[v.children[1]], node_list=node_list) + 1

    v.height = max(h1, h2)
    return v.height


def generate_tree(_array, node_list):
    # add info of each node of a ordered tree
    for each in _array:
        idx, *children = [int(x) for x in each]
        # assert len(children) == 2
        if children != [-1, -1]:
            node_list[idx].type = 'internal node'
            node_list[idx].children = children
            node_list[idx].degree = 1 if (-1 in children) else 2
        else:
            # node_list[idx].type = 'leaf'
            # node_list[idx].degree = 0
            continue

        for child_idx in children:
            if child_idx == -1:
                continue
            node_list[child_idx].parent = idx
            node_list[child_idx].sibling = sum(children) - child_idx

    root_idx = [i for i, x in enumerate(node_list) if x.parent == -1][0]
    node_list[root_idx].type = 'root'

    # add node depth info -- using DFS
    dfs(node_idx=root_idx, node_list=node_list)
    for each in node_list:
        get_height(each, node_list=node_list)

    return node_list


def print_result(_node):
    print('parent = ' + str(_node.parent) + ',', end=' ')
    print('sibling = ' + str(_node.sibling) + ',', end=' ')
    print('degree = ' + str(_node.degree) + ',', end=' ')
    print('depth = ' + str(_node.depth) + ',', end=' ')
    print('height = ' + str(_node.height) + ',', end=' ')
    print(_node.type)
    return None


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(lambda x: x.split(), _input[1:]))
    # assert len(array) == array_length

    init_node_list = [Node() for _ in range(array_length)]
    ans = generate_tree(_array=array, node_list=init_node_list)
    for key, node in enumerate(ans):
        print('node ' + str(key) + ':', end=' ')
        print_result(_node=node)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import sys


class Node(object):
    def __init__(self):
        # init node as a single leaf node.
        self.parent = -1
        self.type = 'leaf'
        self.depth = 0
        self.children = []
        self.degree = 0
        self.sibling = -1
        self.height = 0


def dfs(node_idx, depth=0):
    node_list[node_idx].depth = depth
    for child in node_list[node_idx].children:
        dfs(node_idx=child, depth=depth + 1)
    return None


def get_height(v):
    h1, h2 = 0, 0
    if not v.children:
        return 0

    if v.children[0] != -1:
        h1 = get_height(node_list[v.children[0]]) + 1

    if v.children[1] != -1:
        h2 = get_height(node_list[v.children[1]]) + 1

    v.height = max(h1, h2)
    return v.height


def generate_tree(_array):
    # add info of each node of a ordered tree
    for each in _array:
        idx, *children = [int(x) for x in each]
        # assert len(children) == 2
        if children != [-1, -1]:
            node_list[idx].type = 'internal node'
            node_list[idx].children = children
            node_list[idx].degree = 1 if (-1 in children) else 2
        else:
            # node is leaf
            continue

        for child_idx in children:
            if child_idx == -1:
                continue
            node_list[child_idx].parent = idx
            node_list[child_idx].sibling = sum(children) - child_idx

    root_idx = [i for i, x in enumerate(node_list) if x.parent == -1][0]
    node_list[root_idx].type = 'root'

    # add node depth info -- using DFS
    dfs(root_idx)
    for each in node_list:
        get_height(each)

    return node_list


def print_result(_node):
    print('parent = ' + str(_node.parent) + ',', end=' ')
    print('sibling = ' + str(_node.sibling) + ',', end=' ')
    print('degree = ' + str(_node.degree) + ',', end=' ')
    print('depth = ' + str(_node.depth) + ',', end=' ')
    print('height = ' + str(_node.height) + ',', end=' ')
    print(_node.type)
    return None


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(lambda x: x.split(), _input[1:]))
    # assert len(array) == array_length

    node_list = [Node() for _ in range(array_length)]
    ans = generate_tree(_array=array)
    for key, node in enumerate(ans):
        print('node ' + str(key) + ':', end=' ')
        print_result(_node=node)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import sys


class Node(object):
    def __init__(self):
        # init node as a single leaf node.
        self.parent = -1
        self.type = 'leaf'
        self.depth = 0
        self.children = []
        self.degree = 0
        self.sibling = -1
        self.height = 0


def dfs(node_idx, depth=0):
    node_list[node_idx].depth = depth
    for child in node_list[node_idx].children:
        dfs(node_idx=child, depth=depth + 1)
    return None


def get_height(v):
    h1, h2 = 0, 0
    if not v.children:
        return 0

    if v.children[0] != -1:
        h1 = get_height(node_list[v.children[0]]) + 1

    if v.children[1] != -1:
        h2 = get_height(node_list[v.children[1]]) + 1

    v.height = max(h1, h2)
    return v.height


def generate_tree(_array):
    # add info of each node of a ordered tree
    for each in _array:
        idx, *children = [int(x) for x in each]
        # assert len(children) == 2
        if children != [-1, -1]:
            node_list[idx].type = 'internal node'
            node_list[idx].children = children
            node_list[idx].degree = 1 if (-1 in children) else 2
        else:
            # node is leaf
            continue

        for child_idx in children:
            # if child_idx == -1:
            #     continue
            node_list[child_idx].parent = idx
            node_list[child_idx].sibling = sum(children) - child_idx

    root_idx = [i for i, x in enumerate(node_list) if x.parent == -1][0]
    node_list[root_idx].type = 'root'

    # add node depth info -- using DFS
    dfs(root_idx)
    get_height(node_list[root_idx])

    # for each in node_list:
    #     get_height(each)

    return node_list


def print_result(_node):
    print('parent = ' + str(_node.parent) + ',', end=' ')
    print('sibling = ' + str(_node.sibling) + ',', end=' ')
    print('degree = ' + str(_node.degree) + ',', end=' ')
    print('depth = ' + str(_node.depth) + ',', end=' ')
    print('height = ' + str(_node.height) + ',', end=' ')
    print(_node.type)
    return None


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(lambda x: x.split(), _input[1:]))
    # assert len(array) == array_length

    node_list = [Node() for _ in range(array_length)]
    ans = generate_tree(_array=array)
    for key, node in enumerate(ans):
        print('node ' + str(key) + ':', end=' ')
        print_result(_node=node)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import sys


class Node(object):
    def __init__(self):
        # init node as a single leaf node.
        self.parent = -1
        self.type = 'leaf'
        self.depth = 100
        self.children = []
        self.degree = 0
        self.sibling = -1
        self.height = 0


def get_depth(node_idx, depth=0):
    node_list[node_idx].depth = depth
    for child in node_list[node_idx].children:
        get_depth(node_idx=child, depth=depth + 1)
    return None


# def get_depth(v, depth=0):
#     node_list[node_idx].depth = depth
#     for child in node_list[node_idx].children:
#         get_depth(node_idx=child, depth=depth + 1)
#     return None


def get_height(v):
    h1, h2 = 0, 0

    # leaf's height is 0, children: []
    if not v.children:
        return 0

    if v.children[0] != -1:
        h1 = get_height(node_list[v.children[0]]) + 1

    if v.children[1] != -1:
        h2 = get_height(node_list[v.children[1]]) + 1

    v.height = max(h1, h2)
    return v.height


def generate_tree(_array):
    # add info of each node of a ordered tree
    for each in _array:
        idx, *children = [int(x) for x in each]
        # assert len(children) == 2
        if children != [-1, -1]:
            node_list[idx].type = 'internal node'
            node_list[idx].children = children
            node_list[idx].degree = 1 if (-1 in children) else 2
        else:
            # node is leaf as default, children: []
            continue

        for child_idx in children:
            # non-existent node: no parent or sibling
            if child_idx == -1:
                continue
            node_list[child_idx].parent = idx
            node_list[child_idx].sibling = sum(children) - child_idx

    root_idx = [i for i, x in enumerate(node_list) if x.parent == -1][0]
    node_list[root_idx].type = 'root'

    # add node depth info -- using DFS
    get_depth(root_idx)
    get_height(node_list[root_idx])

    return node_list


def print_result(_node):
    print('parent = ' + str(_node.parent) + ',', end=' ')
    print('sibling = ' + str(_node.sibling) + ',', end=' ')
    print('degree = ' + str(_node.degree) + ',', end=' ')
    print('depth = ' + str(_node.depth) + ',', end=' ')
    print('height = ' + str(_node.height) + ',', end=' ')
    print(_node.type)
    return None


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(lambda x: x.split(), _input[1:]))
    # assert len(array) == array_length

    node_list = [Node() for _ in range(array_length)]
    ans = generate_tree(_array=array)
    for key, node in enumerate(ans):
        print('node ' + str(key) + ':', end=' ')
        print_result(_node=node)

Submission status: Wrong Answer

Solution code:
import sys

nil = -1

class Node:
    def __init__(self):
        self.parent = nil
        self.sibling = nil
        self.degree = 0
        self.left = nil
        self.right = nil
        self.depth = nil
        self.height = nil
        self.ntype = 'leaf'

def set_depth(v, depth):
    bitree[v].depth = depth

    if bitree[v].left != nil:
        set_depth(bitree[v].left, depth + 1)

    if bitree[v].right != nil:
        set_depth(bitree[v].right, depth + 1)

def set_height(v, height):
    bitree[v].height = height

    if bitree[v].parent != nil:
        if bitree[bitree[v].parent].height < bitree[v].height + 1:
            cur = bitree[v].parent
            i = 1

            while cur != nil:
                bitree[cur].height = height + i
                i += 1
                cur = bitree[cur].parent

n = int(input())
bitree = [Node() for i in range(n)]
leaves = set()

for i in range(n):
    line = [int(j) for j in input().split()]
    t_id = line[0]
    bitree[t_id].left = line[1]
    bitree[t_id].right = line[2]
    bitree[t_id].degree = (line[1] != nil) + (line[2] != nil)

    if bitree[t_id].degree > 0:
        bitree[t_id].ntype = 'internal node'

        if bitree[t_id].left != nil:
            bitree[bitree[t_id].left].parent = t_id

        if bitree[t_id].right != nil:
            bitree[bitree[t_id].right].parent = t_id

        if bitree[t_id].degree == 2:
            bitree[bitree[t_id].left].sibling = bitree[t_id].right
            bitree[bitree[t_id].right].sibling = bitree[t_id].left
    else:
        leaves.add(t_id)

for t_id in range(n):
    if bitree[t_id].parent == nil:
        bitree[t_id].ntype = 'root'
        r = t_id
        break

set_depth(r, 0)

for leaf in leaves:
    set_height(leaf, 0)

for t_id in range(n):

    print('node {}: parent = {}, subling = {}, degree = {}, depth = {}, height = {}, {}'.format(
            t_id, bitree[t_id].parent, bitree[t_id].sibling, bitree[t_id].degree,
            bitree[t_id].depth, bitree[t_id].height, bitree[t_id].ntype))

Submission status: Wrong Answer

Solution code:
import sys

nil = -1

class Node:
    def __init__(self):
        self.parent = nil
        self.sibling = nil
        self.degree = 0
        self.left = nil
        self.right = nil
        self.depth = nil
        self.height = nil
        self.ntype = 'leaf'

def set_depth(v, depth):
    bitree[v].depth = depth

    if bitree[v].left != nil:
        set_depth(bitree[v].left, depth + 1)

    if bitree[v].right != nil:
        set_depth(bitree[v].right, depth + 1)

def set_height(v, height):
    bitree[v].height = height

    if bitree[v].parent != nil:
        if bitree[bitree[v].parent].height < bitree[v].height + 1:
            cur = bitree[v].parent
            i = 1

            while cur != nil:
                bitree[cur].height = height + i
                i += 1
                cur = bitree[cur].parent

n = int(input())
bitree = [Node() for i in range(n)]
leaves = set()

for i in range(n):
    line = [int(j) for j in input().split()]
    t_id = line[0]
    bitree[t_id].left = line[1]
    bitree[t_id].right = line[2]
    bitree[t_id].degree = (line[1] != nil) + (line[2] != nil)

    if bitree[t_id].degree > 0:
        bitree[t_id].ntype = 'internal node'

        if bitree[t_id].left != nil:
            bitree[bitree[t_id].left].parent = t_id

        if bitree[t_id].right != nil:
            bitree[bitree[t_id].right].parent = t_id

        if bitree[t_id].degree == 2:
            bitree[bitree[t_id].left].sibling = bitree[t_id].right
            bitree[bitree[t_id].right].sibling = bitree[t_id].left
    else:
        leaves.add(t_id)

for t_id in range(n):
    if bitree[t_id].parent == nil:
        bitree[t_id].ntype = 'root'
        r = t_id
        break

set_depth(r, 0)

for leaf in leaves:
    set_height(leaf, 0)

for t_id in range(n):

    print('node {}: parent = {}, sibling = {}, degree = {}, depth = {}, height = {}, {}'.format(
            t_id, bitree[t_id].parent, bitree[t_id].sibling, bitree[t_id].degree,
            bitree[t_id].depth, bitree[t_id].height, bitree[t_id].ntype))