bool IsBST ( BinTree T )
{
BinTree max,min;
if(!T) return true;
else
{
if(T->Left&&T->Right)
{
max=T->Left;
min=T->Right;
while(max->Right)
max=max->Right;
while(min->Left)
min=min->Left;
if((max->Data<T->Data)&&(min->Data>T->Data))
return (IsBST(T->Left)&&IsBST(T->Right));
else return false;
}
else if(T->Left&&!(T->Right))
{
max=T->Left;
while(max->Right)
max=max->Right;
if(max->Data<T->Data)
return IsBST(T->Left);
else return false;
}
else if(!(T->Left)&&T->Right)
{
min=T->Right;
while(min->Left)
min=min->Left;
if(min->Data>T->Data)
return IsBST(T->Right);
else return false;
}
else if((!(T->Left))&&!(T->Right))
return true;
}
}
习题4.3 是否二叉搜索树(25 分)
最新推荐文章于 2022-12-08 21:49:45 发布