I came across the question where i have to find if the tree is mirror of itself or not. well, the answer on differenet website is completely understandle where it takes two node and check if they are mirror or not:
But I thought of the question in the way where i can first invert the tree(find node of its mirror tree) then compare if both are identical or not.
The Code as follows:
Node* invert(Node* x) {
if(x==NULL) return NULL;
Node* temp = x->left;
x->left = invert(x->right);
x->right = invert(temp);
return x;
}
int isSameTree(Node* A, Node* B) {
if(A==NULL and B==NULL) return 1;
if(A==NULL or B==NULL) return 0;
if(A->data!=B->data) return 0;
return isSameTree(A->left, B->left) and isSameTree(A->right, B->right);
}
bool isSymmetric(struct Node* root)
{
Node* b = invert(root);
return isSameTree(b,root);
}
}But It doesn't work fine. Why?