Check if two binary trees are same or not
suggest change- For example if the inputs are:
Example:1
a)
![](/essential/algorithms/img/be48ce31e9b76a787deb764da10f29af0cff40ff.png)
b)
![](/essential/algorithms/img/70c6ab453502efe719b3d1293d2f876fcc1d0c03.png)
Output should be true.
Example:2
If the inputs are:
a)
![](/essential/algorithms/img/bb8abec4e24a7bbdcf04a4f62b9f49983d8682fe.png)
b)
![](/essential/algorithms/img/15614fc2f154565c087a32dcf26a08a7ef9bf80e.png)
Output should be false.
Pseudo code for the same:
boolean sameTree(node root1, node root2){
if(root1 == NULL && root2 == NULL)
return true;
if(root1 == NULL || root2 == NULL)
return false;
if(root1->data == root2->data
&& sameTree(root1->left,root2->left)
&& sameTree(root1->right, root2->right))
return true;
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents