bool width(node* root,node* root2) { if(root==NULL&&root2==NULL) { return true; } if(root==NULL||root2==NULL) { return false; } if(root->val==root2->val) { return true; } if(root->val!=root2->val) { return false; } return width(root->left,root2->right)&&width(root->right,root2->left); } why does this code not work ???? it is returning true always even if not mirror trees..
The problem seems to be here: if(root->val==root2->val) { return true; } - If your root values are equal , it'll return true. It won't check further. - Remove this code & it should work fine.
Short and Clear explanation , Good job!
Thanks for your nice feedback. Keep Watching.
time complexity would be min(n1, n2)
where n1 is no of nodes in tree1, n2 is no of nodes in tree2... let me know if it's correct
I too think so
Hey, your all videos are great, but can you please upload some more videos on Graphs!!
I'll try to upload on Graph soon. Thanks for your interest.
sir can we find mirror of any one tree and then compare the inorder of both the trees if same then return true else false
No. It will give wrong answer
bool width(node* root,node* root2)
{
if(root==NULL&&root2==NULL)
{
return true;
}
if(root==NULL||root2==NULL)
{
return false;
}
if(root->val==root2->val)
{
return true;
}
if(root->val!=root2->val)
{
return false;
}
return width(root->left,root2->right)&&width(root->right,root2->left);
}
why does this code not work ???? it is returning true always even if not mirror trees..
The problem seems to be here:
if(root->val==root2->val)
{
return true;
}
- If your root values are equal , it'll return true. It won't check further.
- Remove this code & it should work fine.