# Python3 program of iterative traversal based
# method to find common elements in two BSTs.
 
# A utility function to create a new node
 
 
class newNode:
    def __init__(self, key):
        self.key = key
        self.left = self.right = None
 
# Function two print common elements
# in given two trees
 
 
def printCommon(root1, root2):
 
    # Create two stacks for two inorder
    # traversals
    s1 = []
    s2 = []
 
    while 1:
 
        # append the Nodes of first
        # tree in stack s1
        if root1:
            s1.append(root1)
            root1 = root1.left
 
        # append the Nodes of second tree
        # in stack s2
        elif root2:
            s2.append(root2)
            root2 = root2.left
 
        # Both root1 and root2 are NULL here
        elif len(s1) != 0 and len(s2) != 0:
            root1 = s1[-1]
            root2 = s2[-1]
 
            # If current keys in two trees are same
            if root1.key == root2.key:
                print(root1.key, end=" ")
                s1.pop(-1)
                s2.pop(-1)
 
                # move to the inorder successor
                root1 = root1.right
                root2 = root2.right
 
            elif root1.key < root2.key:
 
                # 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
                s1.pop(-1)
                root1 = root1.right
 
                # root2 is set to NULL, because we need
                # new Nodes of tree 1
                root2 = None
            elif root1.key > root2.key:
                s2.pop(-1)
                root2 = root2.right
                root1 = None
 
        # Both roots and both stacks are empty
        else:
            break
 
# A utility function to do inorder traversal
 
 
def inorder(root):
    if root:
        inorder(root.left)
        print(root.key, end=" ")
        inorder(root.right)
 
# A utility function to insert a new Node
# with given key in BST
 
 
def insert(node, key):
 
    # If the tree is empty, return a new Node
    if node == None:
        return newNode(key)
 
    # Otherwise, recur down the tree
    if key < node.key:
        node.left = insert(node.left, key)
    elif key > node.key:
        node.right = insert(node.right, key)
 
    # return the (unchanged) Node pointer
    return node
 
 
# Driver Code
if __name__ == '__main__':
 
    # Create first tree as shown in example
    root1 = None
    root1 = insert(root1, 5)
    root1 = insert(root1, 1)
    root1 = insert(root1, 10)
    root1 = insert(root1, 0)
    root1 = insert(root1, 4)
    root1 = insert(root1, 7)
    root1 = insert(root1, 9)
 
    # Create second tree as shown in example
    root2 = None
    root2 = insert(root2, 10)
    root2 = insert(root2, 7)
    root2 = insert(root2, 20)
    root2 = insert(root2, 4)
    root2 = insert(root2, 9)
 
    print("Tree 1 : ")
    inorder(root1)
    print()
 
    print("Tree 2 : ")
    inorder(root2)
    print()
 
    print("Common Nodes: ")
    printCommon(root1, root2)
 
# This code is contributed by PranchalK

Time complexity: linear
Space complexity: logn