File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -180,6 +180,25 @@ std::vector<int64_t> BinaryTree::inOrderIterative(Node *root) {
180
180
}
181
181
return result;
182
182
}
183
+ void deleteAll (Node *root) {
184
+ if (root) {
185
+ std::stack<Node *> stack;
186
+ stack.push (root);
187
+
188
+ while (!stack.empty ()) {
189
+ const Node *current = stack.top ();
190
+ stack.pop ();
191
+
192
+ if (current->right ) {
193
+ stack.push (current->right );
194
+ }
195
+ if (current->left ) {
196
+ stack.push (current->left );
197
+ }
198
+ delete current;
199
+ }
200
+ }
201
+ }
183
202
} // namespace iterative_tree_traversals
184
203
} // namespace others
185
204
@@ -396,5 +415,7 @@ int main() {
396
415
test6 (binaryTree, root); // run inorder-iterative test on negative values
397
416
std::cout << " \n In-order test on-negative value Passed!" << std::endl;
398
417
418
+ deleteAll (root);
419
+
399
420
return 0 ;
400
421
}
You can’t perform that action at this time.
0 commit comments