【问题标题】:Binary Search Tree, height [closed]二叉搜索树,高度
【发布时间】:2014-09-05 05:36:42
【问题描述】:

这是我在 bst 中的高度函数。 cpp

int IntBinaryTree::getHeight(TreeNode * nodePtr)
{
  if(nodePtr = NULL)
    return 0;
  else
    return (max(1+getHeight(nodePtr->left), 1+getHeight(nodePtr->right)));
}

当我在 main() 中调用它时。我有一个错误。

这是我的 main()

int main {
  IntBinaryTree tree;
   .... 
  tree. getHeight(); 
  return 0; 
}

【问题讨论】:

  • 可能是您没有将参数传递给您的函数吗?

标签: c++ binary-tree binary-search


【解决方案1】:

你没有说是什么错误,但看起来好像变了:

if(nodePtr = NULL)

if(nodePtr == NULL)
           ^^

是你需要的。

【讨论】:

  • 因此,将比较写为NULL == nodePtr 而不是nodePtr == NULL 可能是个好主意。 NULL = nodePtr(注意赋值运算符)不会编译。
猜你喜欢
  • 1970-01-01
  • 2013-10-19
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多