# Flatten Binary Tree in order of Zig Zag traversal

Given a Binary Tree, the task is to flatten it in order of ZigZag traversal of the tree. In the flattened binary tree, the left node of all the nodes must be NULL.

Examples:

```Input:
1
/   \
5     2
/ \   / \
6   4 9   3
Output: 1 2 5 6 4 9 3

Input:
1
\
2
\
3
\
4
\
5
Output: 1 2 3 4 5
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: We will solve this problem by simulating the ZigZag traversal of Binary Tree.
Algorithm:

1. Create two stacks, “c_lev” and “n_lev” and to store the nodes of current and next level Binary tree.
2. Create a variable “prev” and initialise it by parent node.
3. Push right and left children of parent in the c_lev stack.
4. Apply ZigZag traversal. Lets say “curr” is top most element in “c_lev”. Then,
• If ‘curr’ is NULL, continue.
• Else push curr->left and curr->right on the stack “n_lev” in appropriate order. If we are performing left to right traversal then curr->left is pushed first else curr->right is pushed first.
• Set prev = curr.

Below is the implementation of the above approach:

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Node of the binary tree ` `struct` `node { ` `    ``int` `data; ` `    ``node* left; ` `    ``node* right; ` `    ``node(``int` `data) ` `    ``{ ` `        ``this``->data = data; ` `        ``left = NULL; ` `        ``right = NULL; ` `    ``} ` `}; ` ` `  `// Function to flatten Binary tree ` `void` `flatten(node* parent) ` `{ ` `    ``// Queue to store node ` `    ``// for BFS ` `    ``stack c_lev, n_lev; ` ` `  `    ``c_lev.push(parent->left); ` `    ``c_lev.push(parent->right); ` ` `  `    ``bool` `lev = 1; ` `    ``node* prev = parent; ` ` `  `    ``// Code for BFS ` `    ``while` `(c_lev.size()) { ` ` `  `        ``// Size of queue ` `        ``while` `(c_lev.size()) { ` ` `  `            ``// Front most node in ` `            ``// queue ` `            ``node* curr = c_lev.top(); ` `            ``c_lev.pop(); ` ` `  `            ``// Base case ` `            ``if` `(curr == NULL) ` `                ``continue``; ` `            ``prev->right = curr; ` `            ``prev->left = NULL; ` `            ``prev = curr; ` ` `  `            ``// Pushing new elements ` `            ``// in queue ` `            ``if` `(!lev) ` `                ``n_lev.push(curr->left); ` `            ``n_lev.push(curr->right); ` `            ``if` `(lev) ` `                ``n_lev.push(curr->left); ` `        ``} ` `        ``lev = (!lev); ` `        ``c_lev = n_lev; ` `        ``while` `(n_lev.size()) ` `            ``n_lev.pop(); ` `    ``} ` ` `  `    ``prev->left = NULL; ` `    ``prev->right = NULL; ` `} ` ` `  `// Function to print flattened ` `// binary tree ` `void` `print(node* parent) ` `{ ` `    ``node* curr = parent; ` `    ``while` `(curr != NULL) ` `        ``cout << curr->data << ``" "``, curr = curr->right; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``node* root = ``new` `node(1); ` `    ``root->left = ``new` `node(5); ` `    ``root->right = ``new` `node(2); ` `    ``root->left->left = ``new` `node(6); ` `    ``root->left->right = ``new` `node(4); ` `    ``root->right->left = ``new` `node(9); ` `    ``root->right->right = ``new` `node(3); ` ` `  `    ``// Calling required functions ` `    ``flatten(root); ` ` `  `    ``print(root); ` ` `  `    ``return` `0; ` `} `

Output:

```1 2 5 6 4 9 3
```

Time Complexity: O(N)
Space Complexity: O(N) where N is the size of Binary Tree.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.