I think there is some issue in the buildtree because I have submitted the same function on GFG and got AC. But have a look on complete code.
Test 3 failed rest got passed
hey @sk14452, there need a small change in your code.
I have made these changes on your code, you can check them here
Can you justify the changes Please!
hey @sk14452, we are starting with roots of both tree. Now if (!root1 && !root2) this becomes true when both tree are empty (return true as NULL trees are also identical) or we reached to leaves of both trees(return true stating that leaves are identical).
if (root1 && root2) this will be true when we have proper nodes here.
return (structurallyIdentical(root1->left,root2->left) && structurallyIdentical(root1->right,root2->right));
Not any one out of left subtree or right subtree is not identical, false will be returned due to &&
if both left subtree and right subtree are identical, true will be returned.
last return false is denoting the condition that either root1 is NULL or root2 is NULL, therefore cannot be structurally identical.
You might be thinking about when I havent check the data, but question wants structure of these to be identical not both trees are identical.
Thanks! Last line second phrase resolved my doubt! I need more concentration while reading questions.
hey @sk14452 , if your query is resolved. Please mark this doubt as resolved and rate me on the basis of your experience.
rating option will appear when to mark this doubt as resolved
Already done😶…