Skip to content

Commit

Permalink
whenever you allocate memory on the heap using the new keyword you th…
Browse files Browse the repository at this point in the history
…en have to free that memory using delete
  • Loading branch information
Rishabh672003 committed Dec 14, 2023
1 parent 94ff206 commit eb53501
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Data-Structures/Trees/problems/check-for-balanced-bt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -54,5 +62,7 @@ int main() {

bool isBalancedResult = solution.isBalanced(balancedTree);
cout << "tree balanced? " << (isBalancedResult ? "Yes" : "No") << endl;

solution.deleteTree(balancedTree);
return 0;
}
8 changes: 8 additions & 0 deletions Data-Structures/Trees/problems/count-leaves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -37,5 +44,6 @@ int main() {
int result = solution.countLeaves(root);
cout << "Sum of left leaves: " << result << std::endl;

solution.deleteTree(root);
return 0;
}
8 changes: 8 additions & 0 deletions Data-Structures/Trees/problems/depth-of-bt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
}
8 changes: 8 additions & 0 deletions Data-Structures/Trees/problems/sum-of-left-leaves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
}
12 changes: 12 additions & 0 deletions Data-Structures/Trees/problems/symmetric-bt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
}

0 comments on commit eb53501

Please sign in to comment.