Check if two trees are Mirror | A binary tree node ; Given two trees , return true if they are mirror of each other ; Base case : Both empty ; If only one is empty ; Both non - empty , compare them recursively . Note that in recursive calls , we pass left of one tree and right of other tree ; Driver code
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 areMirror ( a , b ) : NEW_LINE INDENT if a is None and b is None : NEW_LINE INDENT return True NEW_LINE DEDENT if a is None or b is None : NEW_LINE INDENT return False NEW_LINE DEDENT return ( a . data == b . data and areMirror ( a . left , b . right ) and areMirror ( a . right , b . left ) ) NEW_LINE DEDENT root1 = Node ( 1 ) NEW_LINE root2 = Node ( 1 ) NEW_LINE root1 . left = Node ( 2 ) NEW_LINE root1 . right = Node ( 3 ) NEW_LINE root1 . left . left = Node ( 4 ) NEW_LINE root1 . left . right = Node ( 5 ) NEW_LINE root2 . left = Node ( 3 ) NEW_LINE root2 . right = Node ( 2 ) NEW_LINE root2 . right . left = Node ( 5 ) NEW_LINE root2 . right . right = Node ( 4 ) NEW_LINE if areMirror ( root1 , root2 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT