题目描述
思路分析
二叉树+递归
代码实现
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
bool HasSubtree(TreeNode* root1, TreeNode* root2) {
if(!root1||!root2) return false;
if(isPart(root1,root2)) return true;
else return HasSubtree(root1->left,root2)||HasSubtree(root1->right,root2);
}
bool isPart(TreeNode* root1,TreeNode* root2){
if(!root2) return true;
if(!root1||root1->val!=root2->val) return false;
return isPart(root1->left,root2->left)&&isPart(root1->right,root2->right);
}
};