-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_103.cpp
More file actions
71 lines (57 loc) · 1.25 KB
/
Day_103.cpp
File metadata and controls
71 lines (57 loc) · 1.25 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
64
65
66
67
68
69
70
71
/*
DAY 103: ZigZag Tree Traversal.
https://www.geeksforgeeks.org/level-order-traversal-in-spiral-form/
QUESTION : 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
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ number of nodes ≤ 10^4
*/
vector <int> zigZagTraversal(Node* root)
{
// Code here
vector<int> ans;
stack<Node*> S1;
stack<Node*> S2;
S1.push(root);
while(!S1.empty() || !S2.empty()) {
while(!S1.empty()) {
Node* top = S1.top();
ans.push_back(top->data);
if(top->left)
S2.push(top->left);
if(top->right)
S2.push(top->right);
S1.pop();
}
while(!S2.empty()) {
Node* top = S2.top();
ans.push_back(top->data);
if(top->right)
S1.push(top->right);
if(top->left)
S1.push(top->left);
S2.pop();
}
}
return ans;
}