Question: Two binary trees s and t are isomorphic if they have the same shape; the values stored in the nodes do not affect whether two trees are isomorphic. In the diagram below, the tree in the middle is not isomorphic to the other trees, but the tree on the right is isomorphic to the tree on the left. Write a method isIsomorphic that returns true if its two tree parameters are isomorphic and false otherwise.
Code:

  
bool isomorphic(struct treenode *treeone, struct treenode *treetwo)
{
if (!treeone && !treetwo)
return true;
if((!treeone && treetwo) || (treeone && !treetwo))
return false;

return (isomorphic(treeone->left, treetwo->left)
&& isomorphic(treeone->right, treetwo->right));
}

Subscribe - To get an automatic feed of all future posts subscribe here, or to receive them via email go here and enter your email address in the box. You can also like us on facebook and follow me on Twitter @akashag1001.