Depth-first search and postorder of a tree rooted at node j.

def __tdfs(j, k, head, next, post, stack):
    """
    Depth-first search and postorder of a tree rooted at node j.
    """
    top = 0
    stack[0] = j
    while (top >= 0):
        p = stack[top]
        i = head[p]
        if i == -1:
            top -= 1
            post[k] = p
            k += 1
        else:
            head[p] = next[i]
            top += 1
            stack[top] = i

    return k