#include <bits/stdc++.h>
using
namespace
std;
class
Node {
public
:
int
data;
Node* left;
Node* right;
Node(
int
d)
{
this
->data = d;
this
->left = NULL;
this
->right = NULL;
}
};
void
inOrder(Node* root, vector<
int
>& sol)
{
if
(root == NULL)
return
;
inOrder(root->left, sol);
sol.push_back(root->data);
inOrder(root->right, sol);
}
void
preOrder(Node* root, vector<
int
>& sol)
{
if
(root == NULL)
return
;
sol.push_back(root->data);
preOrder(root->left, sol);
preOrder(root->right, sol);
}
void
postOrder(Node* root, vector<
int
>& sol)
{
if
(root == NULL)
return
;
postOrder(root->left, sol);
postOrder(root->right, sol);
sol.push_back(root->data);
}
bool
isIdentical(Node* root1, Node* root2)
{
vector<
int
> res1;
vector<
int
> res2;
inOrder(root1, res1);
inOrder(root2, res2);
if
(res1 != res2)
return
false
;
res1.clear();
res2.clear();
preOrder(root1, res1);
preOrder(root2, res2);
if
(res1 != res2)
return
false
;
res1.clear();
res2.clear();
postOrder(root1, res1);
postOrder(root2, res2);
if
(res1 != res2)
return
false
;
return
true
;
}
int
main()
{
struct
Node* root1 =
new
Node(5);
struct
Node* root2 =
new
Node(5);
root1->left =
new
Node(3);
root1->right =
new
Node(8);
root1->left->left =
new
Node(2);
root1->left->right =
new
Node(4);
root2->left =
new
Node(3);
root2->right =
new
Node(8);
root2->left->left =
new
Node(2);
root2->left->right =
new
Node(4);
if
(isIdentical(root1, root2)) {
cout <<
"Both BSTs are identical."
<< endl;
}
else
{
cout <<
"BSTs are not identical."
<< endl;
}
return
0;
}