/*
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* pRoot1, TreeNode* pRoot2)
{ if (pRoot1 == NULL || pRoot2 ==NULL){
return false;
}
bool result= false;
if (pRoot1->val ==pRoot2->val){
result = treeonecntainstree2(pRoot1,pRoot2);
}
if (!result){
result = HasSubtree(pRoot1->left,pRoot2);
}
if (!result){
result = HasSubtree(pRoot1->right,pRoot2);
}
return result;
}
bool treeonecntainstree2(TreeNode* pRoot1, TreeNode* pRoot2){
if (pRoot2==NULL){
return true;
}
if (pRoot1 ==NULL){
return false;
}
if (pRoot1->val != pRoot2->val){
return false;
}
return treeonecntainstree2(pRoot1->left,pRoot2->left) && treeonecntainstree2(pRoot1->right,pRoot2->right) ;}
};
剑指 offer 判断一棵树是否包含另一棵树
猜你喜欢
转载自blog.csdn.net/ttomchy/article/details/104683662
今日推荐
周排行