只需将两个树前序遍历,然后判断是否为字串
class Solution {
public:
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{ if(pRoot2==NULL) return false;
string s1;
pre(pRoot1,s1);
string s2;
pre(pRoot2, s2);
if (s1.find(s2) == string::npos) return false;
return true;
}
void pre(TreeNode * root,string& s)
{
if (root == NULL) return;
s += '0' + root->val;
pre(root->left, s);
pre(root->right, s);
return;
}
};