-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboundary_tree_traversal.cpp
More file actions
45 lines (44 loc) · 1.25 KB
/
Copy pathboundary_tree_traversal.cpp
File metadata and controls
45 lines (44 loc) · 1.25 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
// Problem Link : https://www.geeksforgeeks.org/problems/boundary-traversal-of-binary-tree/1
class Solution {
public:
bool isLeaf(Node* root){
return root && (!root->left && !root->right);
}
void leftBound(Node* root,vector<int>&ans){
Node* t = root->left;
while(t){
if(!isLeaf(t)) ans.push_back(t->data);
if(t->left) t = t->left;
else t = t->right;
}
}
void rightBound(Node* root,vector<int>&ans){
Node* t = root->right;
vector<int>v;
while(t){
if(!isLeaf(t)) v.push_back(t->data);
if(t->right) t = t->right;
else t = t->left;
}
for(int i=v.size()-1;i>=0;i--) ans.push_back(v[i]);
}
void addLeaf(Node* root,vector<int>&ans){
if(isLeaf(root)){
ans.push_back(root->data);
return;
}
if(root->left) addLeaf(root->left,ans);
if(root->right) addLeaf(root->right,ans);
}
vector <int> boundary(Node *root)
{
//Your code here
vector<int> ans;
if(isLeaf(root)) return {1};
ans.push_back(root->data);
leftBound(root,ans);
addLeaf(root,ans);
rightBound(root,ans);
return ans;
}
};