(104). 28.8. IDENTICAL BINARY SEARCH TREES - anishsingh90/Data_Structure_And_Algorithm_In_Cpp_github.io GitHub Wiki
#include <bits/stdc++.h> using namespace std;
struct Node{ int data; Node *left, *right; Node(int val){ data = val; left = NULL; right = NULL; } };
//isIdentical bool isIdentical(Node* root1, Node* root2){ if(root1 == NULL || root2 == NULL){ return true; } else if(root1 == NULL || root2 == NULL){ return false; }
else {
bool cond1 = root1->data == root2->data;
bool cond2 = isIdentical(root1->left, root2->left);
bool cond3 = isIdentical(root1->right, root2->right);
if(cond1 && cond2 && cond3){
return true;
}
else{
return false;
}
}
}
int main(){
//ROOT 1
/*
1
3
/
2 5
/
Node root1 = new Node(1);
root1->right = new Node(3);
root1->right->left = new Node(2);
root1->right->right = new Node(5);
//ROOT 2
/*
1
\
3
/ \
2 8
*/
Node* root2 = new Node(1);
root2->right = new Node(3);
root2->right->left = new Node(2);
root2->right->right = new Node(8);
if(isIdentical(root1,root2)){
cout << "BSTs are identical" << endl;
}
else{
cout << "BSTs are not identical" << endl;
}
return 0;
}
/* OUTPUT: BSTs are not identical */