Print Common Nodes in Two Binary Search Trees | A utility function to create a new node ; Function two print common elements in given two trees ; Create two stacks for two inorder traversals ; append the Nodes of first tree in stack s1 ; append the Nodes of second tree in stack s2 ; Both root1 and root2 are NULL here ; If current keys in two trees are same ; move to the inorder successor ; If Node of first tree is smaller , than that of second tree , then its obvious that the inorder successors of current Node can have same value as that of the second tree Node . Thus , we pop from s2 ; root2 is set to NULL , because we need new Nodes of tree 1 ; Both roots and both stacks are empty ; A utility function to do inorder traversal ; A utility function to insert a new Node with given key in BST ; If the tree is empty , return a new Node ; Otherwise , recur down the tree ; return the ( unchanged ) Node pointer ; Driver Code ; Create first tree as shown in example ; Create second tree as shown in example
class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def printCommon ( root1 , root2 ) : NEW_LINE INDENT s1 = [ ] NEW_LINE s2 = [ ] NEW_LINE while 1 : NEW_LINE INDENT if root1 : NEW_LINE INDENT s1 . append ( root1 ) NEW_LINE root1 = root1 . left NEW_LINE DEDENT elif root2 : NEW_LINE INDENT s2 . append ( root2 ) NEW_LINE root2 = root2 . left NEW_LINE DEDENT elif len ( s1 ) != 0 and len ( s2 ) != 0 : NEW_LINE INDENT root1 = s1 [ - 1 ] NEW_LINE root2 = s2 [ - 1 ] NEW_LINE if root1 . key == root2 . key : NEW_LINE INDENT print ( root1 . key , end = " ▁ " ) NEW_LINE s1 . pop ( - 1 ) NEW_LINE s2 . pop ( - 1 ) NEW_LINE root1 = root1 . right NEW_LINE root2 = root2 . right NEW_LINE DEDENT elif root1 . key < root2 . key : NEW_LINE INDENT s1 . pop ( - 1 ) NEW_LINE root1 = root1 . right NEW_LINE root2 = None NEW_LINE DEDENT elif root1 . key > root2 . key : NEW_LINE INDENT s2 . pop ( - 1 ) NEW_LINE root2 = root2 . right NEW_LINE root1 = None NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT def inorder ( root ) : NEW_LINE INDENT if root : NEW_LINE INDENT inorder ( root . left ) NEW_LINE print ( root . key , end = " ▁ " ) NEW_LINE inorder ( root . right ) NEW_LINE DEDENT DEDENT def insert ( node , key ) : NEW_LINE INDENT if node == None : NEW_LINE INDENT return newNode ( key ) NEW_LINE DEDENT if key < node . key : NEW_LINE INDENT node . left = insert ( node . left , key ) NEW_LINE DEDENT elif key > node . key : NEW_LINE INDENT node . right = insert ( node . right , key ) NEW_LINE DEDENT return node NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root1 = None NEW_LINE root1 = insert ( root1 , 5 ) NEW_LINE root1 = insert ( root1 , 1 ) NEW_LINE root1 = insert ( root1 , 10 ) NEW_LINE root1 = insert ( root1 , 0 ) NEW_LINE root1 = insert ( root1 , 4 ) NEW_LINE root1 = insert ( root1 , 7 ) NEW_LINE root1 = insert ( root1 , 9 ) NEW_LINE root2 = None NEW_LINE root2 = insert ( root2 , 10 ) NEW_LINE root2 = insert ( root2 , 7 ) NEW_LINE root2 = insert ( root2 , 20 ) NEW_LINE root2 = insert ( root2 , 4 ) NEW_LINE root2 = insert ( root2 , 9 ) NEW_LINE print ( " Tree ▁ 1 ▁ : ▁ " ) NEW_LINE inorder ( root1 ) NEW_LINE print ( ) NEW_LINE print ( " Tree ▁ 2 ▁ : ▁ " ) NEW_LINE inorder ( root2 ) NEW_LINE print ( ) NEW_LINE print ( " Common ▁ Nodes : ▁ " ) NEW_LINE printCommon ( root1 , root2 ) NEW_LINE DEDENT