【发布时间】:2012-10-24 01:57:37
【问题描述】:
我正在开发一个函数来查找二叉搜索树的高度。我找到了一种似乎应该可以工作的方法,但我一直收到此错误,我不知道它有什么问题: PA5.exe 中 0x00903417 处的未处理异常:0xC0000005:访问冲突读取位置 0x00000004。
这是我的身高函数...
template <class T>
int BST<T>::height()
{
return displayHeight(mRootNode);
}
template <class T>
int BST<T>::displayHeight(BST<T> *node)
{
if (node = NULL)
{
return 0;
}
int left = displayHeight(node->mLeft);
int right = displayHeight(node->mRight);
if (left > right)
return 1 + left;
else
return 1 + right;
}
这是main函数中的实现……
cout << endl << "height: " << tree.height();
如果我应该包含其他内容,请告诉我。谢谢!
【问题讨论】:
-
我写了相同的代码,但我得到的答案比要求的答案多一个。因此,要在 null 条件下更正它应该返回 -1 而不是返回 0。
标签: c++ data-structures binary-tree