首页 > 其他 > 详细

第十周 求二叉树的高度及宽度

时间:2020-06-16 17:01:45      阅读:37      评论:0      收藏:0      [点我收藏+]

int HeightOfBiTree(BiTree root)
{ //求二叉树的高度
int treeHeight = 0;
if (root != NULL)
{
int leftHeight = HeightOfBiTree(root->lchild);
int rightHeight = HeightOfBiTree(root->rchild);
treeHeight = (leftHeight >= rightHeight)? (leftHeight + 1):(rightHeight + 1);
}

return treeHeight;

}
int WidthOfBiTree(BiTree root)
{ //求二叉树的宽度
if(root==NULL)
{
return 0;
}
int width = 0;
int maxWidth = 0;
queue Q;
BiTree p = NULL;
Q.push(root);
while(!Q.empty())
{
width = Q.size();
if(maxWidth < width)
{
maxWidth = width;
}
for(int i=0; i<width; i++)
{
p = Q.front();
Q.pop();
if(p->lchild)
{
Q.push(p->lchild);
}
if(p->rchild)
{
Q.push(p->rchild);
}
}
}
return maxWidth;
}

第十周 求二叉树的高度及宽度

原文:https://www.cnblogs.com/lkin/p/13141388.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!