白板硬刚就完事儿了
class Solution { public: bool judge(TreeNode* pRoot1, TreeNode* pRoot2){ if(pRoot2==NULL){ return true; }if((pRoot1==NULL && pRoot2!=NULL) || pRoot1->val!=pRoot2->val){ return false; } return judge(pRoot1->left,pRoot2->left)&&judge(pRoot1->right,pRoot2->right); } bool flag=false; void preOrder(TreeNode* pRoot1, TreeNode* pRoot2){ if(pRoot1==NULL)return ; if(pRoot1->val==pRoot2->val){ if(judge(pRoot1,pRoot2)){ flag=true; return ; } } preOrder(pRoot1->left,pRoot2); if(flag)return ; preOrder(pRoot1->right,pRoot2); } bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2){ if(pRoot2==NULL)return false; preOrder(pRoot1,pRoot2); return flag; } };