Python program:
V = 5
WHITE = 1
BLACK = 2
class Node :
    def __init__ ( self ) :
        self . data = 0
        self . left = None
        self . right = None
class subset :
    def __init__ ( self ) :
        self . parent = 0
        self . rank = 0
        self . ancestor = 0
        self . child = 0
        self . sibling = 0
        self . color = 0
class Query :
    def __init__ ( self , L , R ) :
        self . L = L
        self . R = R
def newNode ( data ) :
    node = Node ( )
    node . data = data
    node . left = node . right = None
    return ( node )
def makeSet ( subsets , i ) :
    if ( i < 1 or i > V ) :
        return
    subsets [ i ] . color = WHITE
    subsets [ i ] . parent = i
    subsets [ i ] . rank = 0
    return
def findSet ( subsets , i ) :
    if ( subsets [ i ] . parent != i ) :
        subsets [ i ] . parent = findSet ( subsets , subsets [ i ] . parent )
    return subsets [ i ] . parent
def unionSet ( subsets , x , y ) :
    xroot = findSet ( subsets , x )
    yroot = findSet ( subsets , y )
    if ( subsets [ xroot ] . rank < subsets [ yroot ] . rank ) :
        subsets [ xroot ] . parent = yroot
    elif ( subsets [ xroot ] . rank > subsets [ yroot ] . rank ) :
        subsets [ yroot ] . parent = xroot
    else :
        subsets [ yroot ] . parent = xroot
        ( subsets [ xroot ] . rank ) += 1
def lcaWalk ( u , q , m , subsets ) :
    makeSet ( subsets , u )
    subsets [ findSet ( subsets , u ) ] . ancestor = u
    child = subsets [ u ] . child
    while ( child != 0 ) :
        lcaWalk ( child , q , m , subsets )
        unionSet ( subsets , u , child )
        subsets [ findSet ( subsets , u ) ] . ancestor = u
        child = subsets [ child ] . sibling
    subsets [ u ] . color = BLACK
    for i in range ( m ) :
        if ( q [ i ] . L == u ) :
            if ( subsets [ q [ i ] . R ] . color == BLACK ) :
                print ( " LCA ( % d ▁ % d ) ▁ - > ▁ % d " % ( q [ i ] . L , q [ i ] . R , subsets [ findSet ( subsets , q [ i ] . R ) ] . ancestor ) )
        elif ( q [ i ] . R == u ) :
            if ( subsets [ q [ i ] . L ] . color == BLACK ) :
                print ( " LCA ( % d ▁ % d ) ▁ - > ▁ % d " % ( q [ i ] . L , q [ i ] . R , subsets [ findSet ( subsets , q [ i ] . L ) ] . ancestor ) )
    return
def preprocess ( node , subsets ) :
    if ( node == None ) :
        return
    preprocess ( node . left , subsets )
    if ( node . left != None and node . right != None ) :
        subsets [ node . data ] . child = node . left . data
        subsets [ node . left . data ] . sibling = node . right . data
    elif ( ( node . left != None and node . right == None ) or ( node . left == None and node . right != None ) ) :
        if ( node . left != None and node . right == None ) :
            subsets [ node . data ] . child = node . left . data
        else :
            subsets [ node . data ] . child = node . right . data
    preprocess ( node . right , subsets )
def initialise ( subsets ) :
    for i in range ( 1 , V + 1 ) :
        subsets [ i ] . color = WHITE
    return
def printLCAs ( root , q , m ) :
    subsets = [ subset ( ) for _ in range ( V + 1 ) ]
    initialise ( subsets )
    preprocess ( root , subsets )
    lcaWalk ( root . data , q , m , subsets )
if __name__ == " _ _ main _ _ " :
    root = newNode ( 1 )
    root . left = newNode ( 2 )
    root . right = newNode ( 3 )
    root . left . left = newNode ( 4 )
    root . left . right = newNode ( 5 )
    q = [ Query ( 5 , 4 ) , Query ( 1 , 3 ) , Query ( 2 , 3 ) ]
    m = len ( q )
    printLCAs ( root , q , m )

Index/key variables: child, i, u, xroot, yroot
Iterator variables: i
Class/struct names: Node, Query, subset