-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathZigZag tree.cpp
94 lines (77 loc) · 2.57 KB
/
ZigZag tree.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
/*
ZigZag tree
Given a binary tree, print the zig zag order. In zigzag order, level 1 is printed from left to right, level 2 from right to left and so on.
This means odd levels should get printed from left to right and even level right to left.
Input format:
The first line of input contains data of the nodes of the tree in level order form. The data of the nodes of the tree is separated by space.
If any node does not have a left or right child, take -1 in its place. Since -1 is used as an indication whether the left or right nodes exist,
therefore, it will not be a part of the data of any node.
Output Format:
The binary tree is printed level wise, as described in the task. Each level is printed in new line.
Constraints
Time Limit: 1 second
Sample Input :
5 6 10 2 3 -1 -1 -1 -1 -1 9 -1 -1
Sample Output :
5
10 6
2 3
9
*/
/**********************************************************
Following is the Binary Tree Node class structure
template <typename T>
class BinaryTreeNode {
public :
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;
BinaryTreeNode(T data) {
this -> data = data;
left = NULL;
right = NULL;
}
};
***********************************************************/
#include<stack>
void zigZagOrder(BinaryTreeNode<int> *root) {
// Write your code here
//corner case
if(root == NULL) {
return;
}
stack<BinaryTreeNode<int>*> oddLevel; // At odd level we print from left to right
stack<BinaryTreeNode<int>*> evenLevel; // At odd level we print from right to left
oddLevel.push(root); // considering root at level 1
while(oddLevel.size() or evenLevel.size()) {
while(oddLevel.size()) {
BinaryTreeNode<int> *top = oddLevel.top();
cout << top -> data << " ";
oddLevel.pop();
if(top -> left) {
evenLevel.push(top -> left);
}
if(top -> right) {
evenLevel.push(top -> right);
}
if(oddLevel.empty()){
cout << endl;
}
}
while(evenLevel.size()) {
BinaryTreeNode<int> *top = evenLevel.top();
cout << top -> data << " ";
evenLevel.pop();
if(top -> right) {
oddLevel.push(top -> right);
}
if(top -> left) {
oddLevel.push(top -> left);
}
if(evenLevel.empty()) {
cout << endl;
}
}
}
return;
}