-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboundary_traversal_of_BT.cpp
165 lines (140 loc) · 4.18 KB
/
boundary_traversal_of_BT.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
/*
# Problem Statement: To find the boundary traversal of a BT
---------------------------------------------------------------
# How to think:
* Boundary traversal can be obtained by:
--> Fetching left nodes
--> Fetching leaf nodes
--> Fetching right nodes in reverse
* Can write seperate recursive functions for doing the same
* NOTE: Sometimes, iterative traversal is better when recursive might get complicated(2nd solution)**
---------------------------------------------------------------
*/
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node* left, * right;
};
class Solution {
public:
Node* fetchLeft(Node* root, vector<int>& result) {
if(root == nullptr) {
return nullptr;
}
else if((root->left == nullptr && root->right == nullptr)) {
return root;
}
result.push_back(root->data);
Node* lh = fetchLeft(root->left, result);
if(lh == nullptr) {
Node* rh = fetchLeft(root->right, result);
return rh;
}
return lh;
}
void fetchLeaf(Node* root, vector<int>& result) {
if(root == nullptr) {
return;
}
if(root->left == nullptr && root->right == nullptr) {
result.push_back(root->data);
return;
}
fetchLeaf(root->left, result);
fetchLeaf(root->right, result);
}
Node* fetchRight(Node* root, vector<int>& result, stack<int>& st) {
if(root == nullptr) {
return nullptr;
}
else if((root->left == nullptr && root->right == nullptr)) {
return root;
}
st.push(root->data);
Node* right = fetchRight(root->right, result, st);
if(right == nullptr) {
Node* left = fetchRight(root->left, result, st);
return left;
}
return right;
}
void fetchReverseRight(Node* root, vector<int>& result) {
stack<int> st;
fetchRight(root, result, st);
while(!st.empty()) {
result.push_back(st.top());
st.pop();
}
}
vector <int> boundary(Node *root)
{
vector<int> result;
result.push_back(root->data);
if(root->left == nullptr && root->right == nullptr) {
return result;
}
// left nodes
fetchLeft(root->left, result);
// leaf nodes
fetchLeaf(root, result);
// // reverse right nodes
fetchReverseRight(root->right, result);
return result;
}
};
class Solution {
public:
bool isLeaf(Node* root) {
return (root->left == nullptr && root->right == nullptr);
}
void fetchLeft(Node* root, vector<int>& result) {
Node* curr = root->left;
while(curr) {
if(!isLeaf(curr)) {
result.push_back(curr->data);
}
if(curr->left) curr = curr->left;
else curr = curr->right;
}
}
void fetchLeaf(Node* root, vector<int>& result) {
if(isLeaf(root)) {
result.push_back(root->data);
return;
}
if(root->left) fetchLeaf(root->left, result);
if(root->right) fetchLeaf(root->right, result);
}
void fetchReverseRight(Node* root, vector<int>& result) {
stack<int> st;
Node* curr = root->right;
while(curr) {
if(!isLeaf(curr)) {
st.push(curr->data);
}
if(curr->right) curr = curr->right;
else curr = curr->left;
}
while(!st.empty()) {
result.push_back(st.top());
st.pop();
}
}
vector <int> boundary(Node *root)
{
vector<int> result;
result.push_back(root->data);
if(root->left == nullptr && root->right == nullptr) {
return result;
}
// left nodes
fetchLeft(root, result);
// leaf nodes
fetchLeaf(root, result);
// // reverse right nodes
fetchReverseRight(root, result);
return result;
}
};