Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions others/iterative_tree_traversals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
}
Loading