-
Notifications
You must be signed in to change notification settings - Fork 6
/
BoundaryTraversaOfATree.txt
65 lines (58 loc) · 1.4 KB
/
BoundaryTraversaOfATree.txt
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
class Solution {
public:
vector <int> printBoundary(Node *root)
{
//Your code here
vector<int> ans;
if(root==NULL)
return ans;
ans.push_back(root->data);
lefttree(root->left,ans);
leafnodes(root,ans);
righttree(root->right,ans);
//reverse(ans.begin(),ans.end());
return ans;
}
void lefttree(Node* root,vector<int> &ans)
{
if(!root)
{
return;
}
if(root->left!=NULL)
{
ans.push_back(root->data);
lefttree(root->left,ans);
}
else if(root->right!=NULL)
{
ans.push_back(root->data);
lefttree(root->right,ans);
}
}
void leafnodes(Node* root,vector<int> &ans)
{
if(!root)
return ;
leafnodes(root->left,ans);
if(root->left==NULL && root->right==NULL)
ans.push_back(root->data);
leafnodes(root->right,ans);
}
void righttree(Node* root,vector<int> &ans)
{
if(!root)
return ;
if(root->right!=NULL)
{
righttree(root->right,ans);
ans.push_back(root->data);
}
else
if(root->left!=NULL)
{
righttree(root->left,ans);
ans.push_back(root->data);
}
}
};