how to do the question
Structurally identical tree
The question is to check if the two trees are structurally identical.
Two trees are structurally identical when they have the exact same structure but values of nodes may be same or different. If you draw the tree structure of the two trees given, if their shape is exactly same, then they are structurally identical. If the first tree has a root node and a right child but no left child, then the second tree must also have a root node with right child only.
If you are facing problem in taking inputs, then you can refer this Structurally identical tree
how to code to compare structures of trees
how to compare the structure of the trees whether identical or not
This is what the question asks. Do some tree traversal along with checking.
Your function should be something like this:
bool isidentical(node* root1,node* root2){
if(root1 == NULL && root2 ==NULL){
return true;
}
if(root1==NULL && root2 != NULL){
return false;
}
if(root1 != NULL && root2 == NULL){
return false;
}
//In other cases
return (isidentical(root1->left,root2->left) && isidentical(root1->right,root2->right));
}