Zig-Zag level order traversal of Binary Tree after every K levels
Given a binary tree and an integer K, the task is to print the level order traversal in such a way that first K levels are printed from left to right, next K levels are printed from right to left, then next K levels are from left to right and so on.
Examples:
Input: K = 1
1
/ \
2 3
/ \ /
4 9 8
Output:
1
3 2
4 9 8
Explanation: In the above example, first level is printed from left to right
and the second level is printed from right to left, and then last level is
printed from left to right.Input: K = 3
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ / \ /
8 9 10 11 12
/ \ /
13 14 15Output:
1
2 3
4 5 6 7
12 11 10 9 8
15 14 13
Explanation: In the above example, first 3 levels are printed from left to right
and the last 2 levels are printed from right to left.
Approach: The solution to the problem is based on the following idea:
Start performing level order traversal on the tree from the left most end. After every K level, change the direction of printing the elements.
For this use stack. When the levels are to be printed from the right keep those values in stack and print the stack elements one by one from top. Because of the last in first out property of stack the elements would printed in reverse order.
Follow the steps mentioned below to implement the above idea:
- Use queue to perform level order traversal.
- In each level:
- If it is to be printed from the left, print them and push their child nodes in the queue.
- If this level is to be printed from the right side push the elements in a stack and print them after the whole level is traversed.
- If K levels are covered change the direction of printing from the next one.
Below is the implementation of the above approach.
C++
// C++ code to implement the approach #include <bits/stdc++.h>using namespace std; // Binary Tree nodestruct Node { int data; Node *left, *right;}; // Function that returns a new nodeNode* newNode(int data){ Node* temp = new Node(); temp->data = data; temp->left = temp->right = NULL; return temp;} // Function to print the level order of tree// by reversing the direction of traversal// after every n levelsvoid traverse(Node* root, int n){ // NULL check if (!root) return; // Queue for level order traversal queue<Node*> q; // Stack for // reverse level order traversal stack<Node*> s; // For changing the direction // of traversal bool right2left = false; // To count number of levels int count = 0; q.push(root); while (!q.empty()) { int size = q.size(); count++; while (size--) { root = q.front(); q.pop(); // Prints the nodes from // left to right if (right2left == false) cout << root->data << " "; // Push the nodes into stack // for printing from right to left else s.push(root); if (root->left) q.push(root->left); if (root->right) q.push(root->right); } // If the condition satisfies // prints the nodes from right to left. if (right2left == true) { while (!s.empty()) { cout << s.top()->data << " "; s.pop(); } } // If the count becomes n // then reverse the direction if (count == n) { right2left = !right2left; // Upate the count to 0 count = 0; } cout << endl; }}int main(){ // Create a Binary Tree Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->right->left = newNode(6); root->right->right = newNode(7); root->left->left->left = newNode(8); root->left->left->right = newNode(9); root->left->right->left = newNode(10); root->right->left->right = newNode(11); root->right->right->left = newNode(12); root->left->left->right->left = newNode(13); root->left->left->right->right = newNode(14); root->right->left->right->left = newNode(15); // Specify K to change the direction // after every K levels. int K = 3; traverse(root, K); return 0;} |
1 2 3 4 5 6 7 12 11 10 9 8 15 14 13
Time Complexity: O(N) where N is number of nodes
Auxiliary Space: O(N)

