题目描述
解法:递归(C++)
哪来那么多事就按照递归来找相同树就是了
/**
* 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(t==nullptr) return true;
if(s==nullptr) return false;
return isSametree(s, t) || isSubtree(s->left, t) || isSubtree(s->right, t);
}
bool isSametree(TreeNode* s, TreeNode* t){
if(s==nullptr && t==nullptr) return true;
if(s==nullptr || t==nullptr) return false;
if(s->val!=t->val) return false;
return isSametree(s->left, t->left) && isSametree(s->right, t->right);
}
};