判断一棵二叉树是否是平衡二叉树
判断每个节点的做右子树高度差,递归法
求一颗二叉树的镜像
交换左右孩子节点
template<class T>
struct TreeNode
{
TreeNode* _Lchild;
TreeNode* _Rchild;
T data;
};
size_t Hight(TreeNode* pRoot)
{
if (pRoot==NULL)
{
return 0;
}
size_t lhight=Hight(pRoot->_Lchild)+1;
size_t rhight=Hight(pRoot->_Rchild)+1;
return lhight>rhight ? lhight:rhight;
}
bool IsBanlence(TreeNode* pRoot)
{
if (pRoot==NULL)
{
return true;
}
int Lhight=Hight(pRoot->_Lchild);
int Rhight=Hight(pRoot->_Rchild);
int hight=Lhight-Rhight;
if (hight>=-1 && hight<=1)
{
return IsBanlence(pRoot->_Lchild)&&
IsBanlence(pRoot->_Rchild);
}
else
return false;
}
void Mirror(TreeNode* pRoot)//镜像
{
if (pRoot==NULL)
{
return ;
}
if (pRoot->_Lchild==NULL && pRoot->_Rchild==NULL)
{
return ;
}
swap(pRoot->_Rchild,pRoot->_Lchild);
Mirror(pRoot->_Rchild);
Mirror(pRoot->_Lchild);
}