-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec64_treeques2.cpp
112 lines (90 loc) · 2.53 KB
/
lec64_treeques2.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
// ZIGZAG TRAVERSE
/*
class Solution
{
public:
// Function to store the zig zag order traversal of tree in a list.
vector<int> zigZagTraversal(Node *root)
{
vector<int> result;
if (root == NULL)
return result;
queue<Node *> q;
q.push(root);
bool l2r = true;
while (!q.empty())
{
int size = q.size();
vector<int> temp(size);
for (int i = 0; i < size; i++)
{
Node *frontnode = q.front();
q.pop();
int index = l2r ? i : size - i - 1;
temp[index] = frontnode->data;
if (frontnode->left)
q.push(frontnode->left);
if (frontnode->right)
q.push(frontnode->right);
}
// direction change
l2r = !l2r;
for (auto i : temp)
{
result.push_back(i);
}
}
return result;
}
};
*/
// Boundary traversal
/*
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 == NULL)
return ans;
ans.push_back(root->data);
traverseLeft(root->left, ans);
traverseLeaf(root->left, ans);
traverseLeaf(root->right, ans);
traverseRight(root->right, ans);
}
};
*/