From 7828b8e238ecff9b13f2ac4c4b863ece0e917981 Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Thu, 12 Sep 2024 01:30:09 +0200 Subject: [PATCH] fix: memory leak in `morrisinorder.cpp` (#2729) --- 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; }