-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRightViewOfATree.cpp
189 lines (147 loc) · 4.39 KB
/
RightViewOfATree.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <map>
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 return the
// Right view of the binary tree
vector<int> rightsideView(Node* root) {
// Vector to store
// the result
vector<int> res;
// Get the level order
// traversal of the tree
vector<vector<int>> levelTraversal = levelOrder(root);
// Iterate through each level and
// add the last element to the result
for (auto level : levelTraversal) {
res.push_back(level.back());
}
return res;
}
// Function to return the
// Left view of the binary tree
vector<int> leftsideView(Node* root) {
// Vector to store the result
vector<int> res;
// Get the level order
// traversal of the tree
vector<vector<int>> levelTraversal = levelOrder(root);
// Iterate through each level and
// add the first element to the result
for (auto level : levelTraversal) {
res.push_back(level.front());
}
return res;
}
vector<int> leftSideView(Node* root){
vector<int> arr;
recursionLeft(root,0,arr);
return arr;
}
private:
// Function that returns the
// level order traversal of a Binary tree
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> ans;
// Return an empty vector
// if the tree is empty
if (!root) {
return ans;
}
// Use a queue to perform
// level order traversal
queue<Node*> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
vector<int> level;
// Process each node
// in the current level
for (int i = 0; i < size; i++) {
Node* top = q.front();
level.push_back(top->data);
q.pop();
// Enqueue the left
// child if it exists
if (top->left != NULL) {
q.push(top->left);
}
// Enqueue the right
// child if it exists
if (top->right != NULL) {
q.push(top->right);
}
}
// Add the current
// level to the result
ans.push_back(level);
}
return ans;
}
//Recursive Function to Traversse the Binary Tree
// populate the Left Side View
void recursionLeft(Node* root,int level,vector<int> &arr){
if(root==NULL){
return;
}
if(arr.size()==level){
arr.push_back(root->data);
}
recursionLeft(root->left,level+1,arr);
recursionLeft(root->right,level+1,arr);
}
void recursionRight(Node* root,int level,vector<int> &arr){
if(root==NULL){
return ;
}
if(arr.size()==level){
arr.push_back(root->data);
}
recursionRight(root->right,level+1,arr);
recursionRight(root->left,level+1,arr);
}
};
int main() {
// Creating a sample binary tree
Node* root = new Node(1);
root->left = new Node(2);
root->left->left = new Node(4);
root->left->right = new Node(10);
root->left->left->right = new Node(5);
root->left->left->right->right = new Node(6);
root->right = new Node(3);
root->right->right = new Node(10);
root->right->left = new Node(9);
Solution solution;
// Get the Right View traversal
vector<int> rightView = solution.rightsideView(root);
// Print the result for Right View
cout << "Right View Traversal: ";
for(auto node: rightView){
cout << node << " ";
}
cout << endl;
// Get the Left View traversal
vector<int> leftView = solution.leftsideView(root);
// Print the result for Left View
cout << "Left View Traversal: ";
for(auto node: leftView){
cout << node << " ";
}
cout << endl;
return 0;
}