jain12

convert a tree into its mirror

Mar 18th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 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. void Inorder(Node* root){
  16.   if(root==NULL)
  17.      return;
  18.   Inorder(root->left);
  19.   cout<<" "<<root->data;
  20.   Inorder(root->right);
  21.   }
  22.  
  23. void Mirror(Node* root){
  24.   if(root==NULL)
  25.     return ;
  26.   Node* temp=root->left;
  27.   root->left=root->right;
  28.   root->right=temp;
  29.   Mirror(root->left);
  30.   Mirror(root->right);
  31.   }
  32.  
  33. int main(){
  34.   Node *root=NULL;
  35.   Node *newnode=new Node(1);
  36.   root=newnode;
  37.   root->left=new Node(9);
  38.   root->right=new Node(3);
  39.   (root->left)->left=new Node(2);
  40.   (root->right)->right=new Node(4);
  41.   Inorder(root);
  42.   Mirror(root);
  43.   cout<<endl<<"after making mirror tree :"<<endl;
  44.   Inorder(root);
  45.   return 0;
  46.   }
Add Comment
Please, Sign In to add comment