Problem:
A graph _G_ = ( _V_ , _E_ ) is a data structure where _V_ is a finite set of vertices and _E_ is a binary relation on _V_ represented by a set of edges. Fig. 1 illustrates an example of a graph (or graphs).     **Fig. 1**  A free tree is a connnected, acyclic, undirected graph. A rooted tree is a free tree in which one of the vertices is distinguished from the others. A vertex of a rooted tree is called "node."  Your task is to write a program which reports the following information for each node _u_ of a given rooted tree _T_ :    * node ID of _u_   * parent of _u_   * depth of _u_   * node type (root, internal node or leaf)   * a list of chidlren of _u_  If the last edge on the path from the root _r_ of a tree _T_ to a node _x_ is ( _p_ , _x_ ), then _p_ is the **parent** of _x_ , and _x_ is a **child** of _p_. The root is the only node in _T_ with no parent.  A node with no children is an **external node** or **leaf**. A nonleaf node is an **internal node**  The number of children of a node _x_ in a rooted tree _T_ is called the **degree** of _x_.  The length of the path from the root _r_ to a node _x_ is the **depth** of _x_ in _T_.  Here, the given tree consists of _n_ nodes and evey node has a unique ID from 0 to _n_ -1.  Fig. 2 shows an example of rooted trees where ID of each node is indicated by a number in a circle (node). The example corresponds to the first sample input.     **Fig. 2**

Incorrect submission (verdict: Wrong Answer):
from sys import stdin

class Node(object):
    def __init__(self, parent=None, children=None, name=None, depth=None):
        self.parent = parent
        self.children = children
        self.name = name
        self.depth = depth

def set_depth(nodes, u):
    p = nodes[u].parent
    d = nodes[p].depth
    if d != None:
        nodes[u].depth = d + 1

def read_rooted_tree(n, nodes):
    for _ in range(n):
        i = [int(i) for i in stdin.readline().strip().split()]
        c = i[2:len(i)]
        nodes[i[0]].children = c
        if c:
            nodes[i[0]].name = "internal node"
        else:
            nodes[i[0]].name = "leaf"
        for j in nodes[i[0]].children:
            nodes[j].parent = i[0]
        set_depth(nodes, i[0])

n = int(input())
nodes = [Node() for _ in range(n)]
nodes[0].parent = -1
nodes[0].depth = 0
nodes[0].name = "root"
read_rooted_tree(n, nodes)
for i in range(n):
    print("node {0}: parent = {1}, depth = {2}, {3}, {4}".format(
           i, nodes[i].parent, nodes[i].depth, nodes[i].name, nodes[i].children))


Fixed, accepted submission:
from sys import stdin

class Node(object):
    def __init__(self, parent=None, children=None, name=None):
        self.parent = parent
        self.children = children
        self.name = name

def print_nodes(nodes, n):
    def get_depth(nodes, u):
        p = nodes[u].parent
        nonlocal d
        if p != None and p != -1:
            get_depth(nodes, p)
            d += 1
        return d

    for i in range(n):
        d = 0
        if nodes[i].parent == None:
            nodes[i].parent = -1
            nodes[i].name = "root"
        print("node {0}: parent = {1}, depth = {2}, {3}, {4}".format(
               i, nodes[i].parent, get_depth(nodes, i), nodes[i].name, nodes[i].children))

def read_rooted_tree(nodes, n):
    for _ in range(n):
        i = [int(i) for i in stdin.readline().strip().split()]
        c = i[2:len(i)]
        nodes[i[0]].children = c
        if c:
            nodes[i[0]].name = "internal node"
        else:
            nodes[i[0]].name = "leaf"
        for j in nodes[i[0]].children:
            nodes[j].parent = i[0]

n = int(input())
nodes = [Node() for _ in range(n)]
read_rooted_tree(nodes, n)
print_nodes(nodes, n)