Skip to content

Instantly share code, notes, and snippets.

@Kishy-nivas
Last active June 20, 2018 15:09
Show Gist options
  • Save Kishy-nivas/ba0b95dc53463dde1c21273dd4a3ca16 to your computer and use it in GitHub Desktop.
Save Kishy-nivas/ba0b95dc53463dde1c21273dd4a3ca16 to your computer and use it in GitHub Desktop.
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(p== nullptr and q == nullptr) // reached the end, so far it passed, so return true
return true;
if(p== nullptr || q== nullptr) // failed,before traversing the entire tree
return false;
if(p->val != q->val) // again a failure
return false;
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right); //both of the left and right child must pass the test !
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment