runtime error: member access within null pointer of type ‘Node’ (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:56:15
运行时错误:“Node”类型的空指针内的成员访问(解决方案.cpp)
摘要:未定义的行为程序:未定义的行为程序_加入。
就是没考虑空指针的情况
class Solution {
private:
Node* pre,head;
void dfs(Node* cur){
if(cur==nullptr) return ;
dfs(cur->left);
if(pre!=nullptr) pre->right = cur;
else head = cur;
cur->left = pre;
pre = cur;
dfs(cur->right);
}
public:
Node* treeToDoublyList(Node* root) {
if(root==nullptr) return head; //
dfs(root);
head->left = pre;
pre->right = head;
return head;
}
};
少了这一行就报错if(root==nullptr) return head;