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

posted @ 2018-07-08 17:39 Wasdns 阅读(...) 评论(...) 编辑 收藏