Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class Node{
- public:
- int data;
- Node *left,*right;
- Node(int key){
- data=key;
- left=NULL;
- right=NULL;
- }
- };
- int CheckSimilarity(Node *root1,Node *root2){
- if(root1==NULL && root2==NULL)
- return 1;
- if(root1==NULL || root2==NULL)
- return 0;
- return(CheckSimilarity(root1->left,root2->left) && CheckSimilarity(root1->right,root2->right));
- }
- int main(){
- Node *root1=NULL;
- Node *newnode=new Node(1);
- root1=newnode;
- root1->left=new Node(2);
- root1->right=new Node(3);
- (root1->left)->left=new Node(4);
- (root1->right)->right=new Node(5);
- ((root1->left)->left)->left=new Node(6);
- Node *root2=NULL;
- Node *node=new Node(7);
- root2=node;
- root2->left=new Node(8);
- root2->right=new Node(9);
- (root2->left)->left=new Node(10);
- (root2->right)->right=new Node(11);
- ((root2->left)->left)->left=new Node(12);
- int result=CheckSimilarity(root1,root2);
- if(result==1)
- cout<<"both structures are similar";
- else
- cout<<"both structures are not similar";
- return 0;
- }
Add Comment
Please, Sign In to add comment