Im trying to create a bool function : The function checks and returns if the (sub) trees are completely identical. Ie. that both trees have exactly the same structure (corresponding nodes have the same occurrence of left and right children) and ID in the corresponding nodes.
I got feed back on this code which is when I call this bool function should be in this form identicalTree(root1, root2) in main section and here is correct, but the function should not be this form bool identicalTree(Node* root1, Node* root2), so I dont know where is the errors here??
#include<bits/stdc++.h>
usingnamespace std;
struct Node {
int ID;
Node *left, *right;
};
bool identicalTree(Node* root1, Node* root2){
// if both current nodes are not null
// both of the current nodes should have same value
// the left subtree of both the trees should also be identical
// the right subtree of both the trees should also be identical
if(root1 && root2)
{
if(root1->ID == root2->ID && identicalTree(root1->left, root2->left) && identicalTree(root1->right, root2->right)) returntrue;
returnfalse;
}
// if both tree have current nodes as null nodes
elseif(!root1 && !root2) returntrue;
returnfalse;
}
//////////////////////////////////////
// create node
Node* create(int ID){
Node* tmp = new Node();
tmp->ID = ID;
tmp->left = tmp->right = NULL;
return tmp;
}
/////////////////////////////////
int main()
{
//sub tree 1
Node* root1 = create(4);
root1->left = create (6);
root1->left->right = create (1);
root1-> left ->right->left = create (5);
root1-> left->left = create (2);
root1-> left ->left-> right = create (3);
//sub tree 2
Node* root2 = create(4);
root2->right= create (6);
root2->right-> right= create (2);
root2->right-> right->left = create (3);
root2->right->left = create (1);
root2->right->left->right =create (5);
cout<<(identicalTree(root1, root2) ? "Ye" : "No");
}