/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSubtree(TreeNode* s, TreeNode* t) {
if (!s) {
return false;
} else if (s->val == t->val && matchSubtree(s, t)) {
return true;
}
return isSubtree(s->left, t) || isSubtree(s->right, t);
}
private:
bool matchSubtree(TreeNode* s, TreeNode* t) {
if (!s && !t) {
return true;
} else if (!s || !t) {
return false;
} else if (s->val != t->val) {
return false;
} else {
return matchSubtree(s->left, t->left) && matchSubtree(s->right, t->right);
}
}
};