Check if two trees are Mirror | C ++ program to check if two trees are mirror of each other ; A binary tree node has data , pointer to left child and a pointer to right child ; 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 ; Helper function that allocates a new node ; Driver program to test areMirror ( )
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; int areMirror ( Node * a , Node * b ) { if ( a == NULL && b == NULL ) return true ; if ( a == NULL b == NULL ) return false ; return a -> data == b -> data && areMirror ( a -> left , b -> right ) && areMirror ( a -> right , b -> left ) ; } Node * newNode ( int data ) { Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } int main ( ) { Node * a = newNode ( 1 ) ; Node * b = newNode ( 1 ) ; a -> left = newNode ( 2 ) ; a -> right = newNode ( 3 ) ; a -> left -> left = newNode ( 4 ) ; a -> left -> right = newNode ( 5 ) ; b -> left = newNode ( 3 ) ; b -> right = newNode ( 2 ) ; b -> right -> left = newNode ( 5 ) ; b -> right -> right = newNode ( 4 ) ; areMirror ( a , b ) ? cout << " Yes " : cout << " No " ; return 0 ; }