Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b169269

Browse files
authored
fix: remove memory leak from reverse_binary_tree.cpp (TheAlgorithms#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

Lines changed: 18 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /