mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-02 18:23:06 +08:00
fix: remove memory leak from iterative_tree_traversals.cpp (#2720)
This commit is contained in:
@@ -180,6 +180,25 @@ std::vector<int64_t> BinaryTree::inOrderIterative(Node *root) {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
void deleteAll(Node *root) {
|
||||
if (root) {
|
||||
std::stack<Node *> stack;
|
||||
stack.push(root);
|
||||
|
||||
while (!stack.empty()) {
|
||||
const Node *current = stack.top();
|
||||
stack.pop();
|
||||
|
||||
if (current->right) {
|
||||
stack.push(current->right);
|
||||
}
|
||||
if (current->left) {
|
||||
stack.push(current->left);
|
||||
}
|
||||
delete current;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace iterative_tree_traversals
|
||||
} // namespace others
|
||||
|
||||
@@ -396,5 +415,7 @@ int main() {
|
||||
test6(binaryTree, root); // run inorder-iterative test on negative values
|
||||
std::cout << "\nIn-order test on-negative value Passed!" << std::endl;
|
||||
|
||||
deleteAll(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user