bool containsTree(TreeNode t1, TreeNode t2)
{
if (t2 == NULL)//空树一定是子树
{
return true;
}
return subTree(t1,t2);
}
bool matchTree(TreeNode r1, TreeNode r2)
{
if (r2 == NULL && r1 == NULL)//若两者都空
{
return true;//子树中已无结点
}
//若其中之一为空,但并不同时为空
if (r1 == NULL || r2 == NULL)
{
return false;
}
if (r1.data != r2.data)
return false;
return (matchTree(r1.left, r2.left) && matchTree(r1.right, r2.right));
}
bool subTree(TreeNode r1, TreeNode r2)
{
if (r1 == NULL)
{
return false;//大的树已经空了,还未找到子树
}
if (r1.data == r2.data)
{
if (matchTree(r1, r2))
return true;
}
return (subTree(r1.left, r2) || subTree(r1.right, r2));
}