Skip to content

Commit b169269

Browse files
authored
fix: remove memory leak from reverse_binary_tree.cpp (#2730)
1 parent 7828b8e commit b169269

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

operations_on_datastructures/reverse_binary_tree.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ class BinaryTree {
9191
return pivot;
9292
}
9393

94+
BinaryTree(const BinaryTree&) = delete;
95+
BinaryTree& operator=(const BinaryTree&) = delete;
96+
9497
public:
9598
/**
9699
* @brief Creates a BinaryTree with a root pointing to NULL.
@@ -100,6 +103,21 @@ class BinaryTree {
100103
* @brief Creates a BinaryTree with a root with an initial value.
101104
*/
102105
explicit BinaryTree(int64_t data) { root = new Node(data); }
106+
107+
~BinaryTree() {
108+
std::vector<Node*> nodes;
109+
nodes.emplace_back(root);
110+
while (!nodes.empty()) {
111+
const auto cur_node = nodes.back();
112+
nodes.pop_back();
113+
if (cur_node) {
114+
nodes.emplace_back(cur_node->left);
115+
nodes.emplace_back(cur_node->right);
116+
delete cur_node;
117+
}
118+
}
119+
}
120+
103121
/**
104122
* @brief Adds a new Node to the Binary Tree
105123
*/

0 commit comments

Comments
 (0)