-
Notifications
You must be signed in to change notification settings - Fork 1
/
Boundary_travsl_of_BT_gfg.cpp
73 lines (57 loc) · 1.64 KB
/
Boundary_travsl_of_BT_gfg.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
/* A binary tree Node
struct Node
{
int data;
Node* left, * right;
}; */
class Solution
{
public:
void traverseLeft(Node *root, vector<int> &ans)
{
if ((root == NULL) || (root->left == NULL && root->right == NULL))
return;
ans.push_back(root->data);
if (root->left)
traverseLeft(root->left, ans);
else
traverseLeft(root->right, ans);
}
void traverseLeaf(Node *root, vector<int> &ans)
{
if (root == NULL)
return;
if (root->left == NULL && root->right == NULL)
{
ans.push_back(root->data);
return;
}
traverseLeaf(root->left, ans);
traverseLeaf(root->right, ans);
}
void traverseRight(Node *root, vector<int> &ans)
{
if ((root == NULL) || (root->left == NULL && root->right == NULL))
return;
if (root->right)
traverseRight(root->right, ans);
else
traverseRight(root->left, ans);
ans.push_back(root->data);
}
vector<int> boundary(Node *root)
{
vector<int> ans;
if (root->left == NULL && root->right == NULL)
{
ans.push_back(root->data);
return ans;
}
ans.push_back(root->data);
traverseLeft(root->left, ans);
traverseLeaf(root->left, ans);
traverseLeaf(root->right, ans);
traverseRight(root->right, ans);
return ans;
}
};