From e0dc5bce3e6f2d28fd0f236b33df69113a00a34b Mon Sep 17 00:00:00 2001 From: vil02 <65706193+vil02@users.noreply.github.com> Date: Wed, 11 Sep 2024 17:55:02 +0200 Subject: [PATCH] fix: memory leak in `morrisinorder.cpp` --- data_structures/morrisinorder.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/data_structures/morrisinorder.cpp b/data_structures/morrisinorder.cpp index f1f9e068c9f..c667ccf13fa 100644 --- a/data_structures/morrisinorder.cpp +++ b/data_structures/morrisinorder.cpp @@ -81,6 +81,14 @@ void morrisInorder(Btree *root) { } } +void deleteAll(const Btree *const root) { + if (root) { + deleteAll(root->left); + deleteAll(root->right); + delete root; + } +} + int main() { // Testing morrisInorder funtion Btree *root = NULL; @@ -88,5 +96,6 @@ int main() { for (i = 1; i <= 7; i++) insert(&root, i); cout << "Morris Inorder: "; morrisInorder(root); + deleteAll(root); return 0; }