Symmetric Tree ( Mirror Image of itself ) | Node structure ; Returns True if trees with roots as root1 and root 2 are mirror ; If both trees are empty , then they are mirror images ; For two trees to be mirror images , the following three conditions must be true 1 - Their root node 's key must be same  2 - left subtree of left tree and right subtree  of the right tree have to be mirror images  3 - right subtree of left tree and left subtree  of right tree have to be mirror images   ; If none of the above conditions is true then root1 and root2 are not mirror images ; returns true if the tree is symmetric i . e mirror image of itself ; Check if tree is mirror of itself ; Let 's construct the tree show in the above figure
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def isMirror ( root1 , root2 ) : NEW_LINE INDENT if root1 is None and root2 is None : NEW_LINE INDENT return True NEW_LINE DEDENT if ( root1 is not None and root2 is not None ) : NEW_LINE INDENT if root1 . key == root2 . key : NEW_LINE INDENT return ( isMirror ( root1 . left , root2 . right ) and isMirror ( root1 . right , root2 . left ) ) NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def isSymmetric ( root ) : NEW_LINE INDENT return isMirror ( root , root ) NEW_LINE DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 2 ) NEW_LINE root . left . left = Node ( 3 ) NEW_LINE root . left . right = Node ( 4 ) NEW_LINE root . right . left = Node ( 4 ) NEW_LINE root . right . right = Node ( 3 ) NEW_LINE print " Symmetric " if isSymmetric ( root ) == True else " Not ▁ symmetric " NEW_LINE