diff --git a/Data-Structures/Trees/problems/check-for-balanced-bt.cpp b/Data-Structures/Trees/problems/check-for-balanced-bt.cpp index b0d31e5..171bc3e 100644 --- a/Data-Structures/Trees/problems/check-for-balanced-bt.cpp +++ b/Data-Structures/Trees/problems/check-for-balanced-bt.cpp @@ -39,6 +39,14 @@ class Solution { return max(lh, rh) + 1; } + + void deleteTree(TreeNode* node) { + if (node != NULL) { + deleteTree(node->left); + deleteTree(node->right); + delete node; + } + } }; int main() { @@ -54,5 +62,7 @@ int main() { bool isBalancedResult = solution.isBalanced(balancedTree); cout << "tree balanced? " << (isBalancedResult ? "Yes" : "No") << endl; + + solution.deleteTree(balancedTree); return 0; } diff --git a/Data-Structures/Trees/problems/count-leaves.cpp b/Data-Structures/Trees/problems/count-leaves.cpp index 11db24c..ccb18d6 100644 --- a/Data-Structures/Trees/problems/count-leaves.cpp +++ b/Data-Structures/Trees/problems/count-leaves.cpp @@ -22,6 +22,13 @@ class Solution { return leftLeaves + rightLeaves; } + void deleteTree(TreeNode* node) { + if (node != NULL) { + deleteTree(node->left); + deleteTree(node->right); + delete node; + } + } }; int main() { @@ -37,5 +44,6 @@ int main() { int result = solution.countLeaves(root); cout << "Sum of left leaves: " << result << std::endl; + solution.deleteTree(root); return 0; } diff --git a/Data-Structures/Trees/problems/depth-of-bt.cpp b/Data-Structures/Trees/problems/depth-of-bt.cpp index 6858d39..8ac58a5 100644 --- a/Data-Structures/Trees/problems/depth-of-bt.cpp +++ b/Data-Structures/Trees/problems/depth-of-bt.cpp @@ -34,6 +34,13 @@ class Solution { int rh = minDepth(root->right); return 1 + min(lh, rh); // formula } + void deleteTree(TreeNode* node) { + if (node != NULL) { + deleteTree(node->left); + deleteTree(node->right); + delete node; + } + } }; int main() { @@ -50,5 +57,6 @@ int main() { std::cout << "Max Depth of tree : " << maxDepth << std::endl; std::cout << "Min Depth of tree : " << minDepth << std::endl; + solution.deleteTree(root); return 0; } diff --git a/Data-Structures/Trees/problems/sum-of-left-leaves.cpp b/Data-Structures/Trees/problems/sum-of-left-leaves.cpp index 50c8c52..7d9d3f3 100644 --- a/Data-Structures/Trees/problems/sum-of-left-leaves.cpp +++ b/Data-Structures/Trees/problems/sum-of-left-leaves.cpp @@ -27,6 +27,13 @@ class Solution { return sum; } + void deleteTree(TreeNode* node) { + if (node != NULL) { + deleteTree(node->left); + deleteTree(node->right); + delete node; + } + } }; int main() { @@ -41,5 +48,6 @@ int main() { int result = solution.sumOfLeftLeaves(root); std::cout << "Sum of left leaves: " << result << std::endl; + solution.deleteTree(root); return 0; } diff --git a/Data-Structures/Trees/problems/symmetric-bt.cpp b/Data-Structures/Trees/problems/symmetric-bt.cpp index 0eaf798..cc77409 100644 --- a/Data-Structures/Trees/problems/symmetric-bt.cpp +++ b/Data-Structures/Trees/problems/symmetric-bt.cpp @@ -26,6 +26,15 @@ class Solution { return helper(left->left, right->right) && helper(left->right, right->left); } + // recursively free the memory of all the nodes + // call this function at the end of the program + void deleteTree(TreeNode* node) { + if (node != NULL) { + deleteTree(node->left); + deleteTree(node->right); + delete node; + } + } }; int main() { @@ -45,6 +54,9 @@ int main() { } else { cout << "The tree is not symmetric." << endl; } + // whenever you are allocating on the heap by using the new keyword you + // need to free that memory with the delete keyword + solution.deleteTree(root); return 0; }