Symmetric Tree ( Mirror Image of itself ) | A Binary Tree Node ; returns true if trees with roots as root1 and root2are mirror ; if both trees are empty , then they are mirror image ; 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 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 ; Driver code ; Let us construct the Tree shown in the above figure
class Node { int key ; Node left , right ; Node ( int item ) { key = item ; left = right = null ; } } class BinaryTree { Node root ; boolean isMirror ( Node node1 , Node node2 ) { if ( node1 == null && node2 == null ) return true ; if ( node1 != null && node2 != null && node1 . key == node2 . key ) return ( isMirror ( node1 . left , node2 . right ) && isMirror ( node1 . right , node2 . left ) ) ; return false ; } boolean isSymmetric ( ) { return isMirror ( root , root ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . left . right = new Node ( 4 ) ; tree . root . right . left = new Node ( 4 ) ; tree . root . right . right = new Node ( 3 ) ; boolean output = tree . isSymmetric ( ) ; if ( output == true ) System . out . println ( " Symmetric " ) ; else System . out . println ( " Not ▁ symmetric " ) ; } }