Can you please tell me what is wrong in my concept

question ide:-https://practice.geeksforgeeks.org/problems/determine-if-two-trees-are-identical/1

Sol:- https://ide.geeksforgeeks.org/hGYNhMcnO2

ques:- Check if two trees are identical or not

My sol:- here i amchecking that the inorder traversal of both the trees are same or not

hello @manikjindal49

check now->

Why cannot we only check the data in inorder traversal

…

u can use any traversal technqiue.

here u were not using consdering result returned by smaller subtree that is why it was wrong.

But i am traversing the whole thing and checking only data .Like in inorder traversal first it goes to left subtree and prints all the data and then for right subtree . In this case i am not printing it but i am coparing whther the data is eqal or not

…

yeah …
consider this

1                    1 
  \                     \
    2                   3

here u will compare 1 1 it will match ,then u will arrive at 2,3 it will not match so it will return false. but that false will not be returnd to main function , it will return to calling function which is 1 . but here u are not considering result returned by smaller subtree thats why it is giving wrong answer

No when it will compare the 2 and 3 it will return false as mentioned in the code and the code willnot further execute after returning the false

…

that false will return to 1… and not to the main function
pls dry run… . . .

ok i will check and tell

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.