2014-09-04 47 views
0

这是我在bst中的高度函数。 cpp二进制搜索树,高度

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

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

这是我的主要()

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

什么是错误。 – YoungJohn 2014-09-04 21:34:55

+0

难道你没有将参数传递给你的函数吗? – 2014-09-04 21:54:18

回答

3

你没有说什么错误,但看起来像发生变化:

if(nodePtr = NULL) 

if(nodePtr == NULL) 
      ^^ 

是你所需要的。

+1

由于这个原因,将比较结果写为'NULL == nodePtr'而不是'nodePtr == NULL'是一个好主意。 'NULL = nodePtr'(注意赋值操作符)不会编译。 – Paul 2014-09-04 21:38:13

+0

@保罗,好点。 – 2014-09-04 21:39:15