-
Notifications
You must be signed in to change notification settings - Fork 243
/
BoudaryTraversalOfBinaryTree.cpp
58 lines (49 loc) · 1.45 KB
/
BoudaryTraversalOfBinaryTree.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
// https://practice.geeksforgeeks.org/problems/boundary-traversal-of-binary-tree/1
class Solution {
public:
void getLeftNodes(Node* root, vector<int> &ans){
if(root==NULL)
return;
else if(root->left==NULL && root->right==NULL)
return;
ans.push_back(root->data);
if(root->left){
getLeftNodes(root->left, ans);
}else{
getLeftNodes(root->right, ans);
}
}
void getRightNodes(Node* root, vector<int> &ans){
if(root==NULL)
return;
else if(root->left==NULL && root->right==NULL)
return;
if(root->right){
getRightNodes(root->right, ans);
}else{
getRightNodes(root->left, ans);
}
ans.push_back(root->data);
}
void getLeafNodes(Node* root, vector<int> &ans){
if(root==NULL)
return;
if(root->left==NULL && root->right==NULL){
ans.push_back(root->data);
return;
}
getLeafNodes(root->left, ans);
getLeafNodes(root->right, ans);
}
vector <int> boundary(Node *root)
{
//Your code here
vector<int> ans;
ans.push_back(root->data);
getLeftNodes(root->left, ans);
getLeafNodes(root->left, ans);
getLeafNodes(root->right, ans);
getRightNodes(root->right, ans);
return ans;
}
};