fix: remove memory leak from iterative_tree_traversals.cpp (#2720)

This commit is contained in:
Piotr Idzik
2024-09-02 01:03:19 +02:00
committed by GitHub
parent 54a20b447e
commit 52db277fb6

View File

@@ -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;
}