二叉树深度与宽度

来源:互联网 发布:noteshelf软件功能 编辑:程序博客网 时间:2024/06/07 16:40


#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;
}
0 0
原创粉丝点击