forked from sachuverma/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZigZag Tree Traversal.cpp
More file actions
63 lines (56 loc) · 1.22 KB
/
ZigZag Tree Traversal.cpp
File metadata and controls
63 lines (56 loc) · 1.22 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
ZigZag Tree Traversal
=====================
Given a Binary Tree. Find the Zig-Zag Level Order Traversal of the Binary Tree.
Example 1:
Input:
3
/ \
2 1
Output:
3 1 2
Example 2:
Input:
7
/ \
9 7
/ \ /
8 8 6
/ \
10 9
Output:
7 7 9 8 8 6 9 10
Your Task:
You don't need to read input or print anything. Your task is to complete the function zigZagTraversal() which takes the root node of the Binary Tree as its input and returns a list containing the node values as they appear in the Zig-Zag Level-Order Traversal of the Tree.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 104
*/
vector<int> zigZagTraversal(Node *root)
{
vector<int> ans;
queue<Node *> q;
q.push(root);
int z = 0;
while (q.size())
{
vector<int> arr;
for (int i = q.size(); i > 0; --i)
{
auto curr = q.front();
q.pop();
arr.push_back(curr->data);
if (curr->left)
q.push(curr->left);
if (curr->right)
q.push(curr->right);
}
if (z)
reverse(arr.begin(), arr.end());
z = (z == 0 ? 1 : 0);
for (auto &i : arr)
ans.push_back(i);
}
return ans;
}