您的位置:首页 > 其它

二叉树深度与宽度

2016-07-05 22:06 387 查看
#include "OJ.h"

#include <string.h>

#include <stdio.h>

/*

Description  

         给定一个二叉树,获取该二叉树的宽度深度。

Prototype

         int GetBiNodeInfo(BiNode &head, unsigned int *pulWidth, unsigned int *pulHeight)

Input Param 

         head   需要获取深度的二叉树头结点

Output Param 

         pulWidth   宽度

         pulHeight  高度

Return Value

         0          成功

         1          失败或其他异常

*/

int getwidth(BiNode *head)
{
if(head==NULL)
return 0;
if(head->left==NULL&&head->right==NULL)
return 1;
int wideleft=getwidth(head->left);
int wideright=getwidth(head->right);

        return wideleft+wideright;
}

int getheight(BiNode *head)
{
if(head==NULL)//深度只有0
return 0;

int leftdep=getheight(head->left);
int rightdep=getheight(head->right);

return leftdep>rightdep?(leftdep+1):(rightdep+1);
}

int GetBiNodeInfo(BiNode &head, unsigned int *pulWidth, unsigned int *pulHeight)

{
/*在这里实现功能*/

if(!pulWidth||!pulHeight)
return -1;

    *pulWidth=getwidth(&head);
*pulHeight=getheight(&head);

 

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: