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

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 {
p = Q.front();
Q.pop();
if(p->lchild)
{
Q.push(p->lchild);
}
if(p->rchild)
{
Q.push(p->rchild);
}
}
}
return maxWidth;
}

你可能感兴趣的:(第十周 求二叉树的高度及宽度)