Last active
June 20, 2018 15:09
-
-
Save Kishy-nivas/ba0b95dc53463dde1c21273dd4a3ca16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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