jain12

check whether two trees are mirror of each other or not

Mar 18th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Node{
  5.   public:
  6.       int data;
  7.       Node *left,*right;
  8.       Node(int key){
  9.         data=key;
  10.         left=NULL;
  11.         right=NULL;
  12.         }
  13.   };
  14.  
  15. int CheckMirror(Node *root1,Node *root2){
  16.   if(root1==NULL && root2==NULL)
  17.     return 1;
  18.   if(root1==NULL || root2==NULL)
  19.     return 0;
  20.   return root1->data==root2->data && CheckMirror(root1->left,root2->right) && CheckMirror(root1->right,root2->left);
  21.   }
  22.  
  23. int main(){
  24.   Node *root1=NULL;
  25.   Node *newnode=new Node(1);
  26.   root1=newnode;
  27.   root1->left=new Node(2);
  28.   root1->right=new Node(3);
  29.   (root1->left)->left=new Node(4);
  30.   (root1->right)->right=new Node(5);
  31.  
  32.   Node *root2=NULL;
  33.   Node *node=new Node(1);
  34.   root2=node;
  35.   root2->left=new Node(3);
  36.   root2->right=new Node(2);
  37.   (root2->left)->left=new Node(5);
  38.   (root2->right)->right=new Node(4);
  39.   if(CheckMirror(root1,root2))
  40.     cout<<"structures are mirror of each other ";
  41.   else
  42.     cout<<" structures are not mirror of each other ";
  43.   return 0;
  44.   }
Add Comment
Please, Sign In to add comment