leetcode 编译问题:Line x: member access within null pointer of type 'struct TreeNode'

参考:

LEETCODE 中的member access within null pointer of type 'struct ListNode'

解决 leetcode 编译问题:Line x: member access within null pointer of type 'struct TreeNode'

在leetcode上提交代码后出现编译错误:

Line x: member access within null pointer of type 'struct TreeNode'

原因:在测试过程中,第x行访问的指针为NULL,通常情况下表明程序未对NULL情况作出判断。例如:

int val = root->next->val;

在这一行中,没有加入rootroot->next是否为空的判断,因此需修改为:

if (root != NULL) {
    if (root->next != NULL) {
        int val = root->next->val;
    }
}

2018.7

猜你喜欢

转载自www.cnblogs.com/qq952693358/p/9280717.html