-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Tree.cpp
More file actions
54 lines (49 loc) · 1.22 KB
/
Copy pathBinary_Tree.cpp
File metadata and controls
54 lines (49 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <queue>
using namespace std;
template <typename T>
class Node {
public:
T data;
Node* left;
Node* right;
};
void insert(Node<int>*& root, int value) {
if (!root) {
root = new Node<int>;
root->data = value;
root->left = root->right = nullptr;
} else {
queue<Node<int>*> q;
q.push(root);
while (!q.empty()) {
Node<int>* curr = q.front();
q.pop();
if (curr->left)
q.push(curr->left);
else {
curr->left = new Node<int>;
curr->left->data = value;
curr->left->left = curr->left->right = nullptr;
break;
}
if (curr->right)
q.push(curr->right);
else {
curr->right = new Node<int>;
curr->right->data = value;
curr->right->left = curr->right->right = nullptr;
break;
}
}
}
}
int main() {
Node<int>* root = nullptr;
insert(root, 10);
insert(root, 20);
insert(root, 30);
insert(root, 40);
// Perform other operations (e.g., deletion, traversal) as needed
return 0;
}