-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeftViewOfTree.cpp
More file actions
131 lines (105 loc) · 3.64 KB
/
LeftViewOfTree.cpp
File metadata and controls
131 lines (105 loc) · 3.64 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
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
#include <bits/stdc++.h>
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;
// Call the recursive function
// to populate the right-side view
recursionRight(root, 0, res);
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;
// Call the recursive function
// to populate the left-side view
recursionLeft(root, 0, res);
return res;
}
private:
// Recursive function to traverse the
// binary tree and populate the left-side view
void recursionLeft(Node* root, int level, vector<int>& res){
// Check if the current node is NULL
if(root == NULL){
return;
}
// Check if the size of the result vector
// is equal to the current level
if(res.size() == level){
// If equal, add the value of the
// current node to the result vector
res.push_back(root->data);
}
// Recursively call the function for the
// left child with an increased level
recursionLeft(root->left, level + 1, res);
// Recursively call the function for the
// right child with an increased level
recursionLeft(root->right, level + 1, res);
}
// Recursive function to traverse the
// binary tree and populate the right-side view
void recursionRight(Node* root, int level, vector<int> &res){
// Check if the current node is NULL
if(root == NULL){
return;
}
// Check if the size of the result vector
// is equal to the current level
if(res.size() == level){
// If equal, add the value of the
// current node to the result vector
res.push_back(root->data);
// Recursively call the function for the
// right child with an increased level
recursionRight(root->right, level + 1, res);
// Recursively call the function for the
// left child with an increased level
recursionRight(root->left, level + 1, res);
}
}
};
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;
}