I was doing a question on leetcode which involves convertting a binary tree into a linked list.
Here is the link to the question : https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
My code:
class Solution {
public:
void convert(TreeNode* root) {
if (root == NULL) {
return;
}
if (root->left == NULL && root->right == NULL) {
return;
}
convert(root->left);
convert(root->right);
TreeNode* rgt = root->right;
root->right = root->left;
root->left = NULL;
TreeNode* temp = root->right;
if (temp == NULL) {
root->right = rgt;
}
else {
while (temp->right != NULL) {
temp = temp->right;
}
temp->right = rgt;
}
return;
}
void flatten(TreeNode* root) {
convert(root);
}
};
I keep on getting the error saying: ==31==ERROR: AddressSanitizer: heap-use-after-free on address 0x603000000048 at pc 0x000000372a98 bp 0x7ffc8e114ef0 sp 0x7ffc8e114ee8 READ of size 8 at 0x603000000048 thread T0
How can i fix this issue?