C语言版本,利用迭代来求解相关的高度与宽度。
利用迭代求深度:
当为空时返回0,否则直接返回深度。
int GetDepth(BiNode *root)
{
if(root == NULL)
return 0;
return GetDepth(root->left) > GetDepth(root->right) ? (GetDepth(root->left)+1):(GetDepth(root->right)+1);
}
求宽度:
int GetWidth(BiNode *root)
{
if(root == NULL)
return 1;
if(root->left == NULL && root->right == NULL)
return 1;
else
{
int l = 0;
int r = 0;
if(root->left != NULL)
l = GetWidth(root->left);
if(root->right != NULL)
r = GetWidth(root->right);
return l+r;
}
}