Print the path common to the two paths from the root to the two given nodes | Initialize n1 and n2 as not visited ; structure of a node of binary tree ; This function returns pointer to LCA of two given values n1 and n2 . v1 is set as True by this function if n1 is found v2 is set as True by this function if n2 is found ; Base case ; If either n1 or n2 matches with root 's data, report the presence  by setting v1 or v2 as True and return root (Note that if a key is ancestor of other, then the ancestor key becomes LCA) ; Look for nodes in left and right subtrees ; If both of the above calls return Non - None , then one node is present in one subtree and other is present in other , So this current node is the LCA ; Otherwise check if left subtree or right subtree is LCA ; Returns True if key k is present in tree rooted with root ; Base Case ; If key k is present at root , or in left subtree or right subtree , return True ; Else return False ; This function returns LCA of n1 and n2 only if both n1 and n2 are present in tree , otherwise returns None ; Find lca of n1 and n2 ; Return LCA only if both n1 and n2 are present in tree ; Else return None ; function returns True if there is a path from root to the given node . It also populates arr ' with the given path ; if root is None there is no path ; push the node ' s ▁ value ▁ in ▁ ' arr ; if it is the required node return True ; else check whether there the required node lies in the left subtree or right subtree of the current node ; required node does not lie either in the left or right subtree of the current node Thus , remove current node ' s ▁ value ▁ from ▁ ' arr '  and then return False; ; function to print the path common to the two paths from the root to the two given nodes if the nodes lie in the binary tree ; vector to store the common path ; LCA of node n1 and n2 ; if LCA of both n1 and n2 exists ; then print the path from root to LCA node ; LCA is not present in the binary tree either n1 or n2 or both are not present ; Driver Code
v1 = False NEW_LINE INDENT v2 = False NEW_LINE DEDENT class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def findLCAUtil ( root : Node , n1 : int , n2 : int ) -> Node : NEW_LINE INDENT global v1 , v2 NEW_LINE if ( root is None ) : NEW_LINE INDENT return None NEW_LINE DEDENT if ( root . data == n1 ) : NEW_LINE INDENT v1 = True NEW_LINE return root NEW_LINE DEDENT if ( root . data == n2 ) : NEW_LINE INDENT v2 = True NEW_LINE return root NEW_LINE DEDENT left_lca = findLCAUtil ( root . left , n1 , n2 ) NEW_LINE right_lca = findLCAUtil ( root . right , n1 , n2 ) NEW_LINE if ( left_lca and right_lca ) : NEW_LINE INDENT return root NEW_LINE DEDENT return left_lca if ( left_lca != None ) else right_lca NEW_LINE DEDENT def find ( root : Node , k : int ) -> bool : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( root . data == k or find ( root . left , k ) or find ( root . right , k ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def findLCA ( root : Node , n1 : int , n2 : int ) -> Node : NEW_LINE INDENT global v1 , v2 NEW_LINE lca = findLCAUtil ( root , n1 , n2 ) NEW_LINE if ( v1 and v2 or v1 and find ( lca , n2 ) or v2 and find ( lca , n1 ) ) : NEW_LINE INDENT return lca NEW_LINE DEDENT return None NEW_LINE DEDENT def hasPath ( root : Node , arr : list , x : int ) -> Node : NEW_LINE INDENT if ( root is None ) : NEW_LINE INDENT return False NEW_LINE DEDENT arr . append ( root . data ) NEW_LINE if ( root . data == x ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( hasPath ( root . left , arr , x ) or hasPath ( root . right , arr , x ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT arr . pop ( ) NEW_LINE return False NEW_LINE DEDENT def printCommonPath ( root : Node , n1 : int , n2 : int ) : NEW_LINE INDENT arr = [ ] NEW_LINE lca = findLCA ( root , n1 , n2 ) NEW_LINE if ( lca ) : NEW_LINE INDENT if ( hasPath ( root , arr , lca . data ) ) : NEW_LINE INDENT for i in range ( len ( arr ) - 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " - > " ) NEW_LINE DEDENT print ( arr [ - 1 ] ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " No ▁ Common ▁ Path " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT v1 = 0 NEW_LINE v2 = 0 NEW_LINE root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . left = Node ( 6 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE root . left . right . left = Node ( 8 ) NEW_LINE root . right . left . right = Node ( 9 ) NEW_LINE n1 = 4 NEW_LINE n2 = 8 NEW_LINE printCommonPath ( root , n1 , n2 ) NEW_LINE DEDENT