您的位置:首页 > 其它

判断一棵二叉树是否是平衡二叉树/求一颗二叉树的镜像

2017-09-19 16:33 435 查看

判断一棵二叉树是否是平衡二叉树

判断每个节点的做右子树高度差,递归法

求一颗二叉树的镜像

交换左右孩子节点

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  二叉树 递归
相关文章推荐