I am practicing binary trees from leet code and encountered this error while solving a question
error message:
0xbebebebebebebebe: note: pointer points here
<memory cannot be printed>
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:41:18
On the internet , i found that it usually happens when we don’t allocate memory to left or right pointers and simply assign the value NULL to them.
can someone tell me why is it wrong to do this and what changes should i make in below code to correct it?
CODE
class Solution {
public:
void preOrderTraversal(TreeNode* root,queue<TreeNode*> &st)
{
if(!root)
return;
st.push(root);
preOrderTraversal(root->left,st);
preOrderTraversal(root->right,st);
return;
}
void flatten(TreeNode* root) {
queue<TreeNode*> st;
preOrderTraversal(root,st);
TreeNode* temp=st.front();
st.pop();
while(!st.empty())
{
temp->left=NULL;
auto rightNode=st.front();
st.pop();
temp->right=rightNode;
temp=temp->right;
}
}
};
Problem LInk:-