提问者:小点点

C++程序查找节点总数[已关闭]


为什么我会出现分割错误?

int countLeaves(Node* root)
{
    if (root = NULL) {
        return 0;
    }
    int left = countLeaves(root->left);
    int right = countLeaves(root->right);
    return 1 + left + right;
}

共2个答案

匿名用户

语句中的表达式root=null

if(root=NULL) { return 0; }

root设置为null,并计算为null(分配的值)。

null被视为false,因此返回0;不执行,并且执行在下一条语句之前。

下一个语句是

int left = countLeaves(root->left);

这里,root被设置为null,被取消引用,这将导致分段错误。

匿名用户

要比较root和null,请使用if(root==null)

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|程序|查找|节点|总数|关闭)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?