Merge two BSTs with constant extra space | Node of the binary tree ; A utility function to print Inorder traversal of a Binary Tree ; The function to print data of two BSTs in sorted order ; Base cases ; If the first tree is exhausted simply print the inorder traversal of the second tree ; If second tree is exhausted simply print the inoreder traversal of the first tree ; A temporary pointer currently pointing to root of first tree ; previous pointer to store the parent of temporary pointer ; Traverse through the first tree until you reach the leftmost element , which is the first element of the tree in the inorder traversal . This is the least element of the tree ; Another temporary pointer currently pointing to root of second tree ; Previous pointer to store the parent of second temporary pointer ; Traverse through the second tree until you reach the leftmost element , which is the first element of the tree in inorder traversal . This is the least element of the tree . ; Compare the least current least elements of both the tree ; If first tree 's element is  smaller print it ; If the node has no parent , that means this node is the root ; Simply make the right child of the root as new root ; If node has a parent ; As this node is the leftmost node , it is certain that it will not have a let child so we simply assign this node ' s ▁ right ▁ pointer , ▁ which ▁ can ▁ be ▁ ▁ either ▁ null ▁ or ▁ not , ▁ to ▁ its ▁ parent ' s left pointer . This statement is just doing the task of deleting the node ; recursively call the merge function with updated tree ; If the node has no parent , that means this node is the root ; Simply make the right child of root as new root ; If node has a parent ; Recursively call the merge function with updated tree ; Driver Code ; Print sorted nodes of both trees
class node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def inorder ( root ) : NEW_LINE INDENT if ( root != None ) : NEW_LINE INDENT inorder ( root . left ) NEW_LINE print ( root . data , end = " ▁ " ) NEW_LINE inorder ( root . right ) NEW_LINE DEDENT DEDENT def merge ( root1 , root2 ) : NEW_LINE INDENT if ( not root1 and not root2 ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( not root1 ) : NEW_LINE INDENT inorder ( root2 ) NEW_LINE return NEW_LINE DEDENT if ( not root2 ) : NEW_LINE INDENT inorder ( root1 ) NEW_LINE return NEW_LINE DEDENT temp1 = root1 NEW_LINE prev1 = None NEW_LINE while ( temp1 . left ) : NEW_LINE INDENT prev1 = temp1 NEW_LINE temp1 = temp1 . left NEW_LINE DEDENT temp2 = root2 NEW_LINE prev2 = None NEW_LINE while ( temp2 . left ) : NEW_LINE INDENT prev2 = temp2 NEW_LINE temp2 = temp2 . left NEW_LINE DEDENT if ( temp1 . data <= temp2 . data ) : NEW_LINE INDENT print ( temp1 . data , end = " ▁ " ) NEW_LINE if ( prev1 == None ) : NEW_LINE INDENT merge ( root1 . right , root2 ) NEW_LINE DEDENT else : NEW_LINE INDENT prev1 . left = temp1 . right NEW_LINE merge ( root1 , root2 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( temp2 . data , end = " ▁ " ) NEW_LINE if ( prev2 == None ) : NEW_LINE INDENT merge ( root1 , root2 . right ) NEW_LINE DEDENT else : NEW_LINE INDENT prev2 . left = temp2 . right NEW_LINE merge ( root1 , root2 ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root1 = None NEW_LINE root2 = None NEW_LINE root1 = node ( 3 ) NEW_LINE root1 . left = node ( 1 ) NEW_LINE root1 . right = node ( 5 ) NEW_LINE root2 = node ( 4 ) NEW_LINE root2 . left = node ( 2 ) NEW_LINE root2 . right = node ( 6 ) NEW_LINE merge ( root1 , root2 ) NEW_LINE DEDENT