-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20BoundryTraversal.cpp
171 lines (144 loc) · 5.21 KB
/
20BoundryTraversal.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//The boundary traversal algorithm should be divided into three main parts traversed in the anti-clockwise direction:
//Left Boundary: Traverse the left boundary of the tree. Start from the root and keep moving to the left child; if unavailable, move to the right child. Continue this until we reach a leaf node.
// Bottom Boundary: Traverse the bottom boundary of the tree by traversing the leaf nodes using a simple preorder traversal. We check if the current node is a lead, and if so, its value is added to the boundary traversal array.
// Right Boundary: The right boundary is traversed in the reverse direction, similar to the left boundary traversal starting from the root node and keep moving to the right child; if unavailable, move to the left child. Nodes that are not leaves are pushed into the right boundary array from end to start to ensure that they are added in the reverse direction.
//to check if the present node is a Leaf node or not
#include <iostream>
#include <vector>
using namespace std;
// Node structure for the binary tree
struct Node {
int data;
Node* left;
Node* right;
// Constructor to initialize
// the node with a value
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
class Solution {
public:
// Function to check
// if a node is a leaf
bool isLeaf(Node* root) {
return !root->left && !root->right;
}
// Function to add the
// left boundary of the tree
void addLeftBoundary(Node* root, vector<int>& res) {
Node* curr = root->left;
while (curr) {
// If the current node is not a leaf,
// add its value to the result
if (!isLeaf(curr)) {
res.push_back(curr->data);
}
// Move to the left child if it exists,
// otherwise move to the right child
if (curr->left) {
curr = curr->left;
} else {
curr = curr->right;
}
}
}
// Function to add the
// right boundary of the tree
void addRightBoundary(Node* root, vector<int>& res) {
Node* curr = root->right;
vector<int> temp;
while (curr) {
// If the current node is not a leaf,
// add its value to a temporary vector
if (!isLeaf(curr)) {
temp.push_back(curr->data);
}
// Move to the right child if it exists,
// otherwise move to the left child
if (curr->right) {
curr = curr->right;
} else {
curr = curr->left;
}
}
// Reverse and add the values from
// the temporary vector to the result
for (int i = temp.size() - 1; i >= 0; --i) {
res.push_back(temp[i]);
}
}
// Function to add the
// leaves of the tree
void addLeaves(Node* root, vector<int>& res) {
// If the current node is a
// leaf, add its value to the result
if (isLeaf(root)) {
res.push_back(root->data);
return;
}
// Recursively add leaves of
// the left and right subtrees
if (root->left) {
addLeaves(root->left, res);
}
if (root->right) {
addLeaves(root->right, res);
}
}
// Main function to perform the
// boundary traversal of the binary tree
vector<int> printBoundary(Node* root) {
vector<int> res;
if (!root) {
return res;
}
// If the root is not a leaf,
// add its value to the result
if (!isLeaf(root)) {
res.push_back(root->data);
}
// Add the left boundary, leaves,
// and right boundary in order
addLeftBoundary(root, res);
addLeaves(root, res);
addRightBoundary(root, res);
return res;
}
};
// Helper function to
// print the result
void printResult(const vector<int>& result) {
for (int val : result) {
cout << val << " ";
}
cout << endl;
}
int main() {
// Creating a sample binary tree
Node* root = new Node(3);
root->left = new Node(5);
root->right = new Node(2);
root->left->left = new Node(4);
root->left->left->left = new Node(2);
root->left->left->left->right = new Node(7);
root->left->left->right = new Node(4);
root->left->left->right->left = new Node(4);
root->left->left->right->right = new Node(1);
root->left->right = new Node(7);
root->left->right->left = new Node(5);
root->left->right->left->left = new Node(4);
root->left->right->left->right = new Node(4);
root->left->right->right = new Node(2);
root->left->right->right->right = new Node(1);
root->right->left = new Node(6);
root->right->left->right = new Node(6);
root->right->left->left = new Node(6);
root->right->left->left->left = new Node(1);
root->right->left->left->right = new Node(7);
Solution solution;
// Get the boundary traversal
vector<int> result = solution.printBoundary(root);
// Print the result
cout << "Boundary Traversal: ";
printResult(result);
return 0;
}